java——多线程的实现方式两种办法解决线程赛跑

Posted 高圈圈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java——多线程的实现方式两种办法解决线程赛跑相关的知识,希望对你有一定的参考价值。

多线程的实现方式:demo1、demo2

demo1:继承Thread类,重写run()方法

package thread_test;

public class ThreadDemo1 extends Thread {
    ThreadDemo1(){
        
    }
    ThreadDemo1(String szName){
        super(szName);
    }
    
    //重载run函数
    public void run() {
        for(int count = 1 , row = 1 ; row < 10 ; row ++ , count ++) {
            for(int i = 0 ; i < count ; i ++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        //线程赛跑
        ThreadDemo1 td1 = new ThreadDemo1();
        ThreadDemo1 td2 = new ThreadDemo1();
        ThreadDemo1 td3 = new ThreadDemo1();
        td1.start();
        td2.start();
        td3.start();
    }

}

demo2:实现runnable接口,实现run()方法

package thread_test;

public class ThreadDemo2 implements Runnable{

    public void run() {
        for(int count = 1 , row = 1 ; row < 10 ; row ++ , count ++) {
            for(int i = 0 ; i < count ; i ++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        //存在线程赛跑问题
        Runnable rb1 = new ThreadDemo2();
        Runnable rb2 = new ThreadDemo2();
        Runnable rb3 = new ThreadDemo2();
        Thread td1 = new Thread(rb1);
        Thread td2 = new Thread(rb2);
        Thread td3 = new Thread(rb3);
        td1.start();
        td2.start();
        td3.start();
    }
}

demo3:两种方法解决进程赛跑问题

package thread_test;

//两种方法解决线程赛跑
class ThreadWait extends Thread{

    public ThreadWait() {
        
    }
    
    public ThreadWait(String name) {
        super(name);
    }
    
    @Override
    public void run() {
        for(int count = 1 , row = 1 ; row < 10 ; row ++ , count ++) {
            for(int i = 0 ; i < count ; i ++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

public class ThreadDemo3{
    public static void main(String[] args) {
        ThreadDemo3 td = new ThreadDemo3();
//        td.Method1();
        td.Method2();
    }
    
    public void Method1() {
        ThreadWait tw1 = new ThreadWait();
        ThreadWait tw2 = new ThreadWait();
        tw1.start();
        while(tw1.isAlive()) {
            try{
                Thread.sleep(100);
            }catch(Exception e){
                e.getMessage();
            }
        }
        tw2.start();
    }
    
    public void Method2() {
        ThreadWait tw1 = new ThreadWait();
        ThreadWait tw2 = new ThreadWait();
        tw1.start();
        try {
            tw1.join();
        }catch(Exception e){
            e.toString();
        }
        tw2.start();
    }
}

 

以上是关于java——多线程的实现方式两种办法解决线程赛跑的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程详解多线程龟兔赛跑实力及实现Callable接口

Java学习多线程:线程创建线程状态线程同步线程通信全总结

Java多线程模拟龟兔赛跑

java中线程的两种创建方式

Java多线程实现的四种方式

第70题JAVA高级技术-多线程4(龟兔赛跑-休眠线程)