如何顺序运行多个线程

时间:2017-09-22 10:55:23

标签: java multithreading

我正在进行多线程处理。在我的程序中,我有三个线程打印hello1,线程二打印hello2和线程三打印hello3。我同时启动线程。我希望输出

  

hello1 hello2 hello3 hello1 hello2 hello3等等......

但每次,我正在运行程序,它提供不同的输出,如 hello2 hello1 hello3,hello1 hello3 hello2。 我希望输出的顺序为1 2 3。 代码如下:

package javaapplication2;


public class ThreadDemo {

void MyThread() {
    boolean st = true;
    Thread t1 = new Thread() {
        @Override
        public void run() {

            for (int x = 0; x < 8; x++) {

                try {
                    System.out.println("hello " + 1);

                    wait();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };
    //thread 2
    Thread t2 = new Thread() {
        @Override
        public void run() {

            for (int x = 0; x < 8; x++) {

                try {
                    System.out.println("hello " + 2);
                    //Thread.sleep(2000);
                    wait();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };
    //thraed  3
    Thread t3 = new Thread() {
        @Override
        public void run() {

            for (int x = 0; x < 8; x++) {

                try {
                    System.out.println("hello " + 3);
                    Thread.sleep(2000);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };

  t1.start();
    try{
        t1.join();
    }catch (Exception e){

    }
    t2.start();
    try{
        t2.join();
    }catch (Exception e){

    }
    t3.start();
    try{
        t3.join();
    }catch (Exception e){

    }
}

public static void main(String[] args) {

    new ThreadDemo().MyThread();
}
}

2 个答案:

答案 0 :(得分:1)

您可以使用SynchronousQueue并将其转换为接力赛,每个线程将棒传递到下一个。只有带有操纵杆的线程才能打印其有效负载。

void go() {
    SynchronousQueue<Object> t1ToT2 = new SynchronousQueue<>();
    SynchronousQueue<Object> t2ToT3 = new SynchronousQueue<>();
    SynchronousQueue<Object> t3ToT1 = new SynchronousQueue<>();
    Object theStick = new Object();

    Thread t1 = new Thread(() -> {
        try {
            for (int x = 0; x < 8; x++) {
                Object stick = t3ToT1.take();
                System.out.println("hello " + 1);
                t1ToT2.put(stick);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    });
    //thread 2
    Thread t2 = new Thread(() -> {
        try {
            for (int x = 0; x < 8; x++) {
                Object stick = t1ToT2.take();
                System.out.println("hello " + 2);
                t2ToT3.put(stick);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    });
    //thread  3
    Thread t3 = new Thread(() -> {

        try {
            for (int x = 0; x < 8; x++) {
                Object stick = t2ToT3.take();
                System.out.println("hello " + 3);
                t3ToT1.put(stick);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    });

    t1.start();
    t2.start();
    t3.start();


    try {
        t3ToT1.put(theStick);
        t1.join();
        t2.join();
        t3.join();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

答案 1 :(得分:0)

线程旨在提高程序的性能,当两个或多个线程并行运行以实现共同目标时。因此,没有必要按顺序运行线程。就像你有3个顺序执行的方法一样。在这种情况下没有必要使用线程。

但是,请按以下方式修改程序以获得所需的输出:

public class ThreadDemo {
void MyThread() {
    // boolean st = true;
    Thread t1 = new Thread() {
        @Override
        public void run() {

            for (int x = 0; x < 8; x++) {

                try {
                    System.out.println("hello " + x);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };
    // thread 2
    Thread t2 = new Thread() {
        @Override
        public void run() {

            for (int x = 0; x < 8; x++) {

                try {
                    System.out.println("hello " + x);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };
    // thraed 3
    Thread t3 = new Thread() {
        @Override
        public void run() {

            for (int x = 0; x < 8; x++) {

                try {
                    System.out.println("hello " + x);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };

    t1.start();
    try {
        t1.join();
    } catch (Exception e) {

    }
    t2.start();
    try {
        t2.join();
    } catch (Exception e) {

    }
    t3.start();

}

public static void main(String[] args) {

    new ThreadDemo().MyThread();
 }
}