求教一个Java问题 IllegalMonitorStateException# Java - 爪哇娇娃
A*u
1 楼
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);
}
}
}
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);
}
}
}