0
我想创建单个变量生产者消费者解决方案,但我在Producer的notifyAll()方法中获取IllegalMonitorStateException。 我正在获取同步块中的锁,可能出现了什么问题?IllegalMonitorStateException当共享对象
/**
* Write a description of class SingleProducerConsumer here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SingleProdCons
{
public static void main(String args[])
{
Integer x = 0;
Thread producer = new Thread(new Producer(x),"Producer");
Thread consumer = new Thread(new Consumer(x),"Consumer");
producer.start();
consumer.start();
}
}
class Producer implements Runnable
{
private Integer x;
Producer(Integer x)
{
this.x = x;
}
@Override
public void run()
{
synchronized(x)
{
while(x.equals(0)==false){
//The value of the variable has not been consumed yet , we should wait here till it gets consumed
try{
x.wait();
}catch(InterruptedException ie)
{
System.out.println("Caught Exception : "+ie.getMessage());
}
}
//Else initialze the variable and let it get used by the Consumer then
synchronized(x)
{
x=10;
x.notifyAll();
}
}
}
}
class Consumer implements Runnable
{
private Integer x;
Consumer(Integer x)
{
this.x = x;
}
@Override
public void run()
{
synchronized(x)
{
while(x.equals(0)==true)
{
//The value has not been put by the Producer thread yet hence we should wait here
System.out.println("We are before the method call of the wait on the x object");
try{
x.wait();
}catch(InterruptedException ie)
{
System.out.println("Caught the exception : "+ie.getMessage());
}
}
}
synchronized(x)
{
System.out.println("Consuming : "+x);
x.notifyAll();
}
}
}
Exception in thread "Producer" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at Producer.run(SingleProdCons.java:55)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Producer" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at Producer.run(SingleProdCons.java:55)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Producer" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at Producer.run(SingleProdCons.java:55)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Producer" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at Producer.run(SingleProdCons.java:55)
at java.lang.Thread.run(Thread.java:745)
好吧,这听起来很酷,但你是什么意思,没有用于任何其他purpose.If我使用私人最终矢量sharedQueue它很好地工作。 :) – Chetan
@Chetan:我的意思是说你只用它来实现同步/等待/通知目的......这意味着即使修改了“守护”值,也可以有单个对象。例如,我会强烈建议*反对*,因为'Vector'上的操作本身是同步的,与您正在做的任何事情相关。只要你不得不担心其他代码将要获取/释放(并可能通知/等待)显示器,就很难推断你的代码。 –
你的意思是使用不同的变量进行修改和等待/通知目的会有不同的变化? – Chetan