Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用thread1打印数字1,2,3,使用thread2打印数字4,5,6,使用thread3打印数字7,8,9,再次使用thread1打印数字10,11,12_Java_Multithreading_Wait_Notify - Fatal编程技术网

Java 使用thread1打印数字1,2,3,使用thread2打印数字4,5,6,使用thread3打印数字7,8,9,再次使用thread1打印数字10,11,12

Java 使用thread1打印数字1,2,3,使用thread2打印数字4,5,6,使用thread3打印数字7,8,9,再次使用thread1打印数字10,11,12,java,multithreading,wait,notify,Java,Multithreading,Wait,Notify,我正在尝试用wait和notify编写一个简单的程序,我将在其中创建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;

我正在尝试用wait和notify编写一个简单的程序,我将在其中创建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();
    }
}

在这里,这只是一些变化

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

公共类MyThread2扩展线程{
公共最终静态对象obj=新对象();
int-threadNo;
静态易失性int-run;
静态易失性int计数器=1;
公共MyThread2(int-threadNo){
this.threadNo=threadNo;
}
@凌驾
公开募捐{
同步(obj){
试一试{
同时(计数器<100){
if(threadNoToRun!=threadNo)
obj.wait();
否则{
System.out.println(threadNo+“计数器值为”+计数器);
计数器++;
System.out.println(threadNo+“计数器值为”+计数器);
计数器++;
System.out.println(threadNo+“计数器值为”+计数器);
计数器++;
如果(线程运行==1){
运行时间=2;
}否则如果(threadNoToRun==2){
运行时间=3;
}否则如果(threadnotrun==3){
运行时间=1;
}
obj.notifyAll();
}
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
公共静态void main(字符串参数[]){
/*
*根据需要创建尽可能多的线程。
*/
MyThread2 th1=新的MyThread2(1);
MyThread2 th2=新的MyThread2(2);
MyThread2 th3=新的MyThread2(3);
MyThread2.ThreadNoRun=1;
th1.start();
th2.start();
th3.start();
}
}

在这里,这只是一些变化

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

公共类MyThread2扩展线程{
公共最终静态对象obj=新对象();
int-threadNo;
静态易失性int-run;
静态易失性int计数器=1;
公共MyThread2(int-threadNo){
this.threadNo=threadNo;
}
@凌驾
公开募捐{
同步(obj){
试一试{
同时(计数器<100){
if(threadNoToRun!=threadNo)
obj.wait();
否则{
System.out.println(threadNo+“计数器值为”+计数器);
计数器++;
System.out.println(threadNo+“计数器值为”+计数器);
计数器++;
System.out.println(threadNo+“计数器值为”+计数器);
计数器++;
如果(线程运行==1){
运行时间=2;
}否则如果(threadNoToRun==2){
运行时间=3;
}否则如果(threadnotrun==3){
运行时间=1;
}
obj.notifyAll();
}
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
公共静态void main(字符串参数[]){
/*
*根据需要创建尽可能多的线程。
*/
MyThread2 th1=新的MyThread2(1);
MyThread2 th2=新的MyThread2(2);
MyThread2 th3=新的MyThread2(3);
MyThread2.ThreadNoRun=1;
th1.start();
th2.start();
th3.start();
}
}

我认为您的帖子有两次代码和输出。除此之外,让我来看看你的代码没有任何循环,因此你的线程在第一次迭代后退出。Thread3恰好转到obj.wait(),然后退出run()。我认为您的帖子有两次代码和输出。除此之外,让我来看看你的代码没有任何循环,因此你的线程在第一次迭代后退出。Thread3正好转到obj.wait(),然后退出run()。你可以使用
ThreadNoRun+=1%4
来避免
if
sYah,考虑过解决这个问题,但我不想偏离OP的代码太远:)你可以使用
ThreadNoRun+=1%4
来避免
if
sYah,考虑过解决这个问题,但我不想偏离OP的代码太远:)
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