Java 如何按顺序运行多个线程

Java 如何按顺序运行多个线程,java,multithreading,Java,Multithreading,我正在处理多线程。在我的程序中,我有三个线程一个打印hello1,两个线程二个打印hello2和三个线程三个打印hello3。我正在同时启动线程。我希望输出是 hello1 hello2 hello3 hello1 hello2 hello3等等 但每次,我都在运行程序,它会给出不同的输出,比如 你好,你好,你好。 我希望输出是在一个1 2 3的序列中。 代码如下所示 package javaapplication2; public class ThreadDemo { void MyTh

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

hello1 hello2 hello3 hello1 hello2 hello3等等

但每次,我都在运行程序,它会给出不同的输出,比如 你好,你好,你好。 我希望输出是在一个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();
}
}
PackageJavaApplication2;
公共类线程演示{
void MyThread(){
布尔值st=真;
线程t1=新线程(){
@凌驾
公开募捐{
对于(int x=0;x<8;x++){
试一试{
System.out.println(“hello”+1);
等待();
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
};
//线程2
线程t2=新线程(){
@凌驾
公开募捐{
对于(int x=0;x<8;x++){
试一试{
System.out.println(“hello”+2);
//《睡眠》(2000年);
等待();
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
};
//色雷斯3
线程t3=新线程(){
@凌驾
公开募捐{
对于(int x=0;x<8;x++){
试一试{
System.out.println(“hello”+3);
《睡眠》(2000年);
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
};
t1.start();
试一试{
t1.join();
}捕获(例外e){
}
t2.start();
试一试{
t2.连接();
}捕获(例外e){
}
t3.start();
试一试{
t3.join();
}捕获(例外e){
}
}
公共静态void main(字符串[]args){
新的ThreadDemo().MyThread();
}
}

线程旨在提高程序的性能,当两个或多个线程并行运行以实现一个共同目标时,会发生这种情况。因此,没有必要按顺序运行线程。这就像你有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();
 }
}
公共类ThreadDemo{
void MyThread(){
//布尔值st=真;
线程t1=新线程(){
@凌驾
公开募捐{
对于(int x=0;x<8;x++){
试一试{
System.out.println(“hello”+x);
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
};
//线程2
线程t2=新线程(){
@凌驾
公开募捐{
对于(int x=0;x<8;x++){
试一试{
System.out.println(“hello”+x);
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
};
//色雷斯3
线程t3=新线程(){
@凌驾
公开募捐{
对于(int x=0;x<8;x++){
试一试{
System.out.println(“hello”+x);
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
};
t1.start();
试一试{
t1.join();
}捕获(例外e){
}
t2.start();
试一试{
t2.连接();
}捕获(例外e){
}
t3.start();
}
公共静态void main(字符串[]args){
新的ThreadDemo().MyThread();
}
}

您可以使用
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();
    }
}
void go(){
SynchronousQueue t1ToT2=新的SynchronousQueue();
SynchronousQueue t2ToT3=新的SynchronousQueue();
SynchronousQueue t3ToT1=新的SynchronousQueue();
Object theStick=新对象();
线程t1=新线程(()->{
试一试{
对于(int x=0;x<8;x++){
Object stick=t3ToT1.take();
System.out.println(“hello”+1);
T12.放(棒);
}
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
});
//线程2
线程t2=新线程(()->{
试一试{
对于(int x=0;x<8;x++){
Object stick=t1ToT2.take();
System.out.println(“hello”+2);
t2ToT3.放(棒);
}
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
});
//线程3
线程t3=新线程(()->{
试一试{
对于(int x=0;x<8;x++){
Object stick=t2ToT3.take();
System.out.println(“hello”+3);
t3ToT1.放(棒);
}
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
});
t1.start();
t2.start();
t3.start();
试一试{
t3ToT1.放(条);
t1.join();
t2.连接();
t3.join();
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
}

如果要按顺序运行线程,那么使用线程有什么意义?您只是忽略了在同步块之外调用
wait()
所产生的所有异常吗?您需要适当的同步,看看CyclicBarrier类的人我讨厌这些问题吗。它们看起来总是像是结构不良的多线程101大学课程问题,因为它们会