首页 > 解决方案 > 使用 thread1 打印数字 1,2,3,使用 thread2 打印数字 4,5,6,使用 thread3 打印数字 7,8,9,再使用 thread1 打印 10,11,12

问题描述

我正在尝试编写一个带有等待和通知的简单程序,我将在其中创建 3 个线程。

第一个线程应该打印 1、2、3。

第二个线程应该打印 4、5、6。

第三个线程应该打印 7、8、9。

之后,第一个线程应该打印 10、11、12 等等。

下面是同一练习的示例代码,但我无法打印所需的输出。

public class MyThread2 extends Thread {
    
    public final static Object obj = new Object();
    
    int threadNo;   
    static volatile int threadNoToRun;
    static volatile int counter = 1;
    
    public MyThread2(int threadNo){
        this.threadNo= threadNo;
    }
    
    @Override
    public void run() {
        synchronized (obj) {
                try {
                    if(threadNoToRun != threadNo)
                        obj.wait();
                    else{
                        for(int i = 1 ; i < 4 ; i++){
                            if(threadNoToRun == threadNo){
                                System.out.println(threadNo + " counter value is "+counter);
                                counter++;
                                System.out.println(threadNo + " counter value is "+counter);
                                counter++;
                                System.out.println(threadNo + " counter value is "+counter);
                                counter++;
                                if(threadNoToRun == 1){
                                    threadNoToRun = 2;
                                }
                                else if(threadNoToRun == 2){
                                    threadNoToRun = 3;
                                }
                                else if(threadNoToRun == 3){
                                    threadNoToRun = 1;
                                }
                            }
                        }
                        
                        obj.notifyAll();                            
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            
        }    
    }
    
    public static void main (String args[]) {
    
        /*
         * Creating as many threads as needed.
         */
        MyThread2 th1 = new MyThread2(1);
        MyThread2 th2 = new MyThread2(2);
        MyThread2 th3 = new MyThread2(3);
        MyThread2.threadNoToRun = 1;
        th1.start();
        th2.start();
        th3.start();
    }
}

输出如下所示:

1 counter value is 1
1 counter value is 2
1 counter value is 3
2 counter value is 4
2 counter value is 5
2 counter value is 6

标签: javamultithreadingwaitnotify

解决方案


在这里,只是一些变化。

尽管如此,我必须指出,这种并发不会提高计算速度。始终只有一个线程处于活动状态。

public class MyThread2 extends Thread {

    public final static Object obj = new Object();

    int threadNo;
    static volatile int threadNoToRun;
    static volatile int counter = 1;

    public MyThread2(int threadNo) {
        this.threadNo = threadNo;
    }

    @Override
    public void run() {
        synchronized (obj) {
            try {
                while (counter < 100) {
                    if (threadNoToRun != threadNo)
                        obj.wait();
                    else {
                        System.out.println(threadNo + " counter value is " + counter);
                        counter++;
                        System.out.println(threadNo + " counter value is " + counter);
                        counter++;
                        System.out.println(threadNo + " counter value is " + counter);
                        counter++;
                        if (threadNoToRun == 1) {
                            threadNoToRun = 2;
                        } else if (threadNoToRun == 2) {
                            threadNoToRun = 3;
                        } else if (threadNoToRun == 3) {
                            threadNoToRun = 1;
                        }

                        obj.notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {

        /*
         * Creating as many threads as needed.
         */
        MyThread2 th1 = new MyThread2(1);
        MyThread2 th2 = new MyThread2(2);
        MyThread2 th3 = new MyThread2(3);
        MyThread2.threadNoToRun = 1;
        th1.start();
        th2.start();
        th3.start();
    }
}

推荐阅读