java - 使用线程 1 打印数字 1,2,3,使用线程 2 打印数字 4,5,6,使用线程 3 打印数字 7,8,9,使用线程 1 打印数字 10,11,12

标签 java multithreading wait notify

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

The first thread should print 1, 2, 3.

The second thread should print 4, 5, 6.

The third thread should print 7, 8, 9.

After that, the first thread should print 10, 11, 12 and so on.

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

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

最佳答案

在这里,只是进行了一些更改。

尽管如此,我必须指出,这种并发并不会提高计算速度。任何时候只有一个线程处于 Activity 状态。

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();
    }
}

关于java - 使用线程 1 打印数字 1,2,3,使用线程 2 打印数字 4,5,6,使用线程 3 打印数字 7,8,9,使用线程 1 打印数字 10,11,12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56316047/

相关文章:

multithreading - Go-尝试创建超过一定数量的goroutine时出现段错误问题

c++ - 参数 `NumberOfConcurrentThreads`在 `CreateIoCompletionPort`中是如何使用的

java - Swing repaint() 在循环或线程中不起作用

java : Does wait() release lock from synchronized block

linux - 在开始另一个之前等待 bash 脚本作业完成

Python 中的 Java Fluent Wait

java - 如何在 Java 中进行 COM 互操作?

java - @NamedQuery 优于 @NamedNativeQuery

java - 使 JDBC 插入查询更快

java - 如何配置Oracle云证书?