由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 求教一个Java问题 IllegalMonitorStateException
相关主题
一个JAVA程序请教求大牛指导这道题。结果是什么?
关于Java一个小程序的结果java 里在 main 外定义函数为什么必须要static?
请教pthread producer-consumer问题java小问题
给大家出个多进程的题go里面channel和wait group用法比较
java synchronized 问题C# 的不定长度的ARRAY?
"short" in Java【讨论】问一道很简单的C++题。。。。 (转载)
这段Java代码能运行吗?A C++ inheritance question!
菜鸟请教C问题请教一个concurrentlinkedqueue的用法问题
相关话题的讨论汇总
话题: producer话题: intbuffer话题: buffer话题: consumer话题: num
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个concurrentlinkedqueue的用法问题java synchronized 问题
再来一个C++模板问题"short" in Java
题2这段Java代码能运行吗?
问个C++ virtual function的问题 (转载)菜鸟请教C问题
一个JAVA程序请教求大牛指导这道题。结果是什么?
关于Java一个小程序的结果java 里在 main 外定义函数为什么必须要static?
请教pthread producer-consumer问题java小问题
给大家出个多进程的题go里面channel和wait group用法比较
相关话题的讨论汇总
话题: producer话题: intbuffer话题: buffer话题: consumer话题: num