繁体   English   中英

按顺序运行线程

[英]Running threads in sequence

假设下面有下面的类,如何强制依次依次执行三个线程? (等待对方终止)

public class MyRunnable implements Runnable{

    @Override
    public void run() {

        System.out.println("Thread 1 :First Thread started");
    }

    public static Runnable delay(){
        Runnable r = new Runnable(){

            @Override
            public void run() {  // running state

                System.out.println("Thread 2: loading second thread..");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 2: System loaded");
            }

        }; // finished state
        return r;
    }

    public static Runnable waiting(){
        Runnable r = new Runnable(){

            @Override
            public void run() { // running state

                System.out.println("Thread 3: waiting..");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 3: OK");

            }

        }; // finished state
        return r;
    }


    public static void main(String[] args) throws InterruptedException{

       Thread thread1 = new Thread(new MyRunnable());      
       Thread thread2 = new Thread(delay()); 
       Thread thread3 = new Thread(waiting()); // (initial state)

       thread1.start();
       thread2.start();
       thread3.start();

    }


}

是的,但是为什么要这么做?

public static void main(String[] args) throws InterruptedException{

    Thread thread1 = new Thread(new MyRunnable());      
    Thread thread2 = new Thread(delay()); 
    Thread thread3 = new Thread(waiting()); // (initial state)

    thread1.start();
    thread1.join();
    thread2.start();
    thread2.join();
    thread3.start();
    thread3.join();
}

其他方式(无线程):

public static void main(String[] args) throws InterruptedException{

    new MyRunnable().run();
    delay().run(); 
    waiting().run();
} 

您的代码执行以下操作:

Main thread      thread-1        thread-2       thread-3
    V
    |
    + . . . . . . > V
    + . . . . . . . | . . . . . . > V
    + . . . . . . . | . . . . . . . | . . . . . . > V
    X               |               |               |
                    X               |               |
                                    |               |
                                    X               |
                                                    |
                                                    X    

您要求这样做(因为线程可以并行化任务,并且您不想对其并行化,所以没有意义!):

Main thread      thread-1        thread-2       thread-3
    V
    |
    + . . . . . . > V
    |               |
    |<--------------X
    + . . . . . . . . . . . . . . > V
    |                               |
    |                               |
    |                               |
    |                               |
    |<------------------------------X
    + . . . . . . . . . . . . . . . . . . . . . . > V
    |                                               |
    |                                               |
    |                                               |
    |                                               |
    |                                               |
    |                                               |
    |<----------------------------------------------X
    X

启动线程后,放入thread.join () 这将等待线程返回,然后再开始下一个线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM