由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 求教一个Java问题 IllegalMonitorStateException
相关主题
有关thread in Java在一个函数里把arraylist设为null 但是有问题
请教一个简单的问题线程问题。
Java练习题 5简单问题
who can help me with this dummy Question??interesting
线程hardy会一直等待下去么?java reflecton question: how to represent a String[] class?
condional variable thread sync 问题请教个garbage collector问题
extending generic class , but not mentioning its parameterized type?问个primitive type的问题
help "java.lang.NoSuchMethodError"问个autoboxing的问题
相关话题的讨论汇总
话题: producer话题: intbuffer话题: buffer话题: consumer话题: num
进入Java版参与讨论
1 (共1页)
A**u
发帖数: 2458
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);
}
}
}
1 (共1页)
进入Java版参与讨论
相关主题
问个autoboxing的问题线程hardy会一直等待下去么?
初学者code请教 (大牛莫取笑)condional variable thread sync 问题
java beginner questionextending generic class , but not mentioning its parameterized type?
local变量被赋值了几次?help "java.lang.NoSuchMethodError"
有关thread in Java在一个函数里把arraylist设为null 但是有问题
请教一个简单的问题线程问题。
Java练习题 5简单问题
who can help me with this dummy Question??interesting
相关话题的讨论汇总
话题: producer话题: intbuffer话题: buffer话题: consumer话题: num