java – wait()/ notify()无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – wait()/ notify()无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4037字,纯文字阅读大概需要6分钟。
内容图文
![java – wait()/ notify()无法正常工作](/upload/InfoBanner/zyjiaocheng/773/c2f91fde06da4ca3a335df6552b0ed4d.jpg)
我有一个ConsumerProducer对象,我想从两个不同的线程获取锁.课程如下:
public class ConsumerProducer {
public String stringPool = null;
public void put(String s){
stringPool = s;
}
public String get(){
String ret = stringPool;
stringPool = null;
return ret;
}
}
线程impl类如下:
public class WaitNotifyTest implements Runnable {
private String threadType;
public ConsumerProducer cp;
public static volatile int i = 1;
public WaitNotifyTest(String threadType, ConsumerProducer cp) {
this.threadType = threadType;
this.cp = cp;
}
public static void main(String[] args) throws InterruptedException {
ConsumerProducer cp = new ConsumerProducer();
WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);
Thread t1 = new Thread(test1);
Thread t2 = new Thread(test2);
t1.start();
t2.start();
t1.join();
t2.join();
}
@Override
public void run() {
while (true) {
if (threadType.equalsIgnoreCase("Consumer")) {
synchronized (cp) {
try {
if (null != cp.get()) {
cp.wait();
}
consume();
System.out.println("notify from Consumer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
synchronized (cp) {
try {
if (null == cp.get()) {
cp.wait();
}
produce();
System.out.println("notify from Producer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (i == 5) {
break;
}
i++;
}
}
public void consume() {
System.out.println("Putting: Counter" + i);
cp.put("Counter" + i);
}
public void produce() {
System.out.println("getting: " + cp.get());
}
}
但是,当我运行代码时,它面临着某种死锁,它就像是打印一样
Putting: Counter3
notify from Consumer
有些事情发生了严重错误,但我无法确定.请帮忙.
解决方法:
你的消费者正在做生产者的工作,生产者正在做消费者的工作.
交换责任并修改条件等.请参阅下面的代码.
>当没有任何东西可以获得时,消费者会等待,他会释放cp的锁定.这样生产者就有机会进入同步区块.
>制片人只在没有任何东西或者他会等待的情况下制作.之后,他将释放cp的锁定.这样消费者就有机会进入同步区块.
>消费者就是把事情赶走的人.
>制片人是把事情放在桌面上的人.
>根据你的评论.你想把Counter从1改为5,所以你应该只在Producer线程中添加i.如何控制两个线程的增加?
>您不判断它是消费者还是生产者从cp对象调用get(),而是将null赋给stringPool.这显然是错误的,并且会使消费者从公共空间中获得空位.我添加了一个新方法clearString(),它将公共空间设置为null,只有当消费者使用了产品时.
public class WaitNotifyTest implements Runnable {
private String threadType;
public ConsumerProducer cp;
public static volatile int i = 0;
public WaitNotifyTest(String threadType, ConsumerProducer cp) {
this.threadType = threadType;
this.cp = cp;
}
public static void main(String[] args) throws InterruptedException {
ConsumerProducer cp = new ConsumerProducer();
WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);
Thread t1 = new Thread(test1);
Thread t2 = new Thread(test2);
t1.start();
t2.start();
t1.join();
t2.join();
}
@Override
public void run() {
while (true) {
if (threadType.equalsIgnoreCase("Consumer")) {
synchronized (cp) {
try {
/*
* Consumer will wait when there is nothing to get and he will release the lock of cp.
* So that producer has change to go into the synchronized block.
*/
if (null == cp.get()) {
cp.wait();
}
consume();
System.out.println("notify from Consumer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
synchronized (cp) {
try {
/*
* Producer only produce when there is nothing or he will wait. At the same time, he will release the lock of cp.
* So that consumer has chance to go into the synchronized block.
*/
if (null != cp.get()) {
cp.wait();
}
i++;
produce();
System.out.println("notify from Producer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (i == 5) {
break;
}
}
}
public void consume() {
System.out.println("getting: " + cp.get());
cp.clearString();
}
public void produce() {
System.out.println("Putting: Counter" + i);
cp.put("Counter" + i);
}}
另请参阅ConsumerProducer类.
public class ConsumerProducer {
public String stringPool = null;
public void put(String s){
stringPool = s;
}
public String get(){
return stringPool;
}
public void clearString(){
stringPool = null;
}
}
内容总结
以上是互联网集市为您收集整理的java – wait()/ notify()无法正常工作全部内容,希望文章能够帮你解决java – wait()/ notify()无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。