gpt4 book ai didi

java - 所有值仅通过一个线程打印

转载 作者:行者123 更新时间:2023-12-02 05:15:23 25 4
gpt4 key购买 nike

正在等待再次启动(处于等待状态)的 Thread- 0 即使在另一个线程点击通知后也没有启动。有人可以帮助我完成以下代码。

尝试使用两个线程 thread-0、thread-1 打印数字,但是一旦线程进入等待状态,即使在另一个线程调用notify后它也不会启动

package com.chan.newFeature;
class ValuePrinter implements Runnable
{

private volatile static int maxValue=1;
private int reminder;
public ValuePrinter(int reminder) {
this.reminder = reminder;
}
public ValuePrinter() {

}

@Override
public void run() {

while(maxValue<20){
synchronized (this){
// System.out.println("maxValue "+maxValue);
/*if (maxValue%2==0)
printEven();
else
printOdd();*/
if (maxValue%2==reminder){
try {
System.out.println("Inside Synchronized Context Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);
this.wait();
System.out.println("Inside Synchronized Context after wait Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);

} catch (InterruptedException e) {
e.printStackTrace();
}
}
// System.out.println("Thread.currentThread().getName()"+Thread.currentThread().getName());
System.out.println("Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue+":"+reminder);
this.notify();
System.out.println("Thread.currentThread().getName() :");
maxValue=maxValue+1;
}
}
}


}







public class OddEvenExample {


public static void main(String[] args) {
ValuePrinter valuePrinter=new ValuePrinter(2);
ValuePrinter valuePrinter2=new ValuePrinter(1);
Thread t1=new Thread(valuePrinter);
Thread t2=new Thread(valuePrinter2);
t1.start();
t2.start();
}
}

Thread.currentThread().getName() :Thread-0:1:2
Inside Synchronized Context Thread.currentThread().getName() :Thread-1:1
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:2:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:3:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:4:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:5:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:6:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:7:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:8:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:9:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:10:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:11:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:12:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:13:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:14:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:15:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:16:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:17:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:18:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:19:2
Thread.currentThread().getName() :

最佳答案

我编辑了我的答案:这个“用notifyAll()替换notify()”不是正确的答案,但问题在这里:Why can't I use notifyAll() to wake up a waiting thread?您正在创建两个对象,它们是独立的并且不在同一个锁中。简单来说,你的notify仅指那个特定的对象线程。

在这里,我得到了你想要的答案:

static Object lock = new Object();

@Override
public void run() {

while(maxValue<50){
synchronized (lock){
// System.out.println("maxValue "+maxValue);
/*if (maxValue%2==0)
printEven();
else
printOdd();*/
if (maxValue%2==reminder){
try {
System.out.println("Inside Synchronized Context Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);
lock.wait();
System.out.println("Inside Synchronized Context after wait Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);

} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue+":"+reminder);
lock.notifyAll();
maxValue=maxValue+1;
}
}

}

为公共(public)锁添加一个静态对象属性,以便锁定该对象而不是自线程。如果这不起作用,请告诉我。

关于java - 所有值仅通过一个线程打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com