A**u 发帖数: 2458 | 1 【 以下文字转载自 Java 讨论区 】
发信人: Augu (奥古), 信区: Java
标 题: 求教一个Java问题 IllegalMonitorStateException
发信站: BBS 未名空间站 (Tue Dec 11 11:31:48 2012, 美东)
import java.util.*;
import java.util.concurrent.locks.*;
public class PC_unsyn
{
public static void main(String[] args)
{
IntBuffer b = new IntBuffer();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
p.setName("Producer");
c.setName("Consumer");
p.start();
c.start();
}
}
class IntBuffer
{
private int index;
private int[] buffer = new int[8];
private Lock bufferLock = new ReentrantLock();
private Condition range;
public IntBuffer()
{
index = 0;
range = bufferLock.newCondition();
}
public void add(int num)
{
bufferLock.lock();
try
{
while(index == buffer.length - 1)
{
range.wait();
}
buffer[index++] = num;
range.signalAll();
}
catch(InterruptedException e)
{
}
finally
{
bufferLock.unlock();
}
}
public int remove()
{
bufferLock.lock();
int ret = 0;
try
{
while(index == 0)
{
range.wait();
}
ret = buffer[--index];
range.signalAll();
}
catch(InterruptedException e)
{
}
finally
{
bufferLock.unlock();
}
return ret;
}
}
class Producer extends Thread
{
private IntBuffer buffer;
public Producer(IntBuffer buffer)
{
this.buffer = buffer;
}
public void run()
{
Random r = new Random();
while(true)
{
int num = r.nextInt();
buffer.add(num);
System.out.println("Producer " + num);
}
}
}
class Consumer extends Thread
{
private IntBuffer buffer;
public Consumer(IntBuffer buffer)
{
this.buffer = buffer;
}
public void run()
{
while(true)
{
int num = buffer.remove();
System.out.println("Consumer " + num);
}
}
}
------------------------------------------------------------------------
这段code,为什么老抛出
-en Exception in thread "Consumer" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)Producer 1755009559
at java.lang.Object.wait(Object.java:502)Producer 1901194634
at IntBuffer.remove(PC_unsyn.java:58)Producer -2122660656
Producer -593401757 at Consumer.run(PC_unsyn.java:105)
Producer -1893332279
Producer 152012877
Producer 914672944
Exception in thread "Producer" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at IntBuffer.add(PC_unsyn.java:37)
at Producer.run(PC_unsyn.java:87)
----------------------------------------------------------------------------
我google,说 IllegalMonitorStateException是由于
Thrown to indicate that a thread has attempted to wait on an object's
monitor or to notify other threads waiting on an object's monitor without
owning the specified monitor.
但是我用lock获取了资源啊
多谢
--------------------------------------------------
如果是下面代码,就没有问题
import java.util.*;
public class PC_unsyn
{
public static void main(String[] args)
{
IntBuffer b = new IntBuffer();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
p.start();
c.start();
}
}
class IntBuffer
{
private int index;
private int[] buffer = new int[8];
public synchronized void add(int num)
{
if ( index == buffer.length - 1 )
{
try
{
wait();
}
catch ( InterruptedException e)
{
}
}
buffer[index++] = num;
notifyAll();
}
public synchronized int remove()
{
if (index == 0)
{
try
{
wait();
}
catch ( InterruptedException e)
{
}
}
int ret = buffer[--index];
notifyAll();
return ret;
}
}
class Producer extends Thread
{
private IntBuffer buffer;
public Producer(IntBuffer buffer)
{
this.buffer = buffer;
}
public void run()
{
Random r = new Random();
while(true)
{
int num = r.nextInt();
buffer.add(num);
System.out.println("Producer " + num);
}
}
}
class Consumer extends Thread
{
private IntBuffer buffer;
public Consumer(IntBuffer buffer)
{
this.buffer = buffer;
}
public void run()
{
while(true)
{
int num = buffer.remove();
System.out.println("Consumer " + num);
}
}
} | N******7 发帖数: 1297 | 2 you should use range.await(), not range.wait() | A**u 发帖数: 2458 | 3 大牛,多谢啦
我再学习一下await, wait diff
调了好久
【在 N******7 的大作中提到】 : you should use range.await(), not range.wait()
| N******7 发帖数: 1297 | 4 wait is old way to use on Object, await is what pairs with signal or
signalAll on Condition. | A**u 发帖数: 2458 | 5 en Thanks a lot
【在 N******7 的大作中提到】 : wait is old way to use on Object, await is what pairs with signal or : signalAll on Condition.
|
|