我使用线程(等待和通知)功能创建了一个生产者消费者程序。代码是 -
/**
* Message.java ( Common object )
*/
package threads;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author chouhan_r
*
*/
public class Message {
private String message;
ReentrantLock lock = new ReentrantLock();
ReentrantLock takelock = new ReentrantLock();
private boolean empty =true;
public void put(String message){
lock.lock();
while(!empty){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
empty = false;
this.message = message;
notifyAll();
lock.unlock();
}
public String take(){
takelock.lock();
while(empty){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
empty = true;
notifyAll();
takelock.unlock();
return message;
}
}
/**
* Producer.java
*/
package threads;
import java.util.Random;
public class Producer implements Runnable{
private Message message;
public Producer(Message message) {
this.message = message;
}
@Override
public void run() {
String[] impInfo = {"Hello", "I am here", "why are you doing this ??", "No problem" };
Random random = new Random();
for(String str : impInfo){
message.put(str);
try {
Thread.sleep(random.nextInt(3000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
message.put("Finished");
}
}
/**
* Consumer.java
*/
package threads;
public class Consumer implements Runnable{
private Message message;
public Consumer(Message message) {
this.message = message;
}
@Override
public void run() {
System.out.println(message);
String msg = message.take();
while(!"Finished".equalsIgnoreCase(msg)){
System.out.println(msg);
msg = message.take();
}
}
}
=====================
/**
* Main Program
*/
package threads;
public class ProConsTest {
public static void main(String[] args) {
Message message = new Message();
new Thread(new Consumer(message)).start();
new Thread(new Producer(message)).start();
}
}
我正在尝试使用ReentrantLock执行此代码,但它给了我跟随错误 -
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at threads.Message.take(Message.java:39)
at threads.Consumer.run(Consumer.java:11)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at threads.Message.put(Message.java:31)
at threads.Producer.run(Producer.java:16)
at java.lang.Thread.run(Unknown Source)
我在这里做错了什么?当我将Message.java中的方法标记为synchronized时,它完美无缺。我们可以只使用Reentrant lock ??
运行此代码答案 0 :(得分:4)
Object.wait
和Object.notify
连接到内部锁定功能,该功能与synchronized
块和方法一起使用。
如果您想使用Lock
,则无法使用这两种方法。相反,您必须create a Condition
并在其上调用await()
,该signal()
应与在同一Condition
实例上调用documentation of Condition
的另一个线程配对。请注意,与Object.wait
和Object.notify
不同,每Condition
可以有多个Lock
,等待特定条件的线程将被调用{{1}的线程唤醒} 在同一个 signal
实例上。
但是,与内部锁定功能一样,在调用Condition
上的方法之前,必须拥有关联的锁。如果是Condition
,则必须拥有写锁定。
不幸的是,ReadWriteLock
从Condition
继承了wait
和notify
方法,所以永远不要混淆它们很重要,这需要特别小心,因为很难区分名称,Object
和wait
。
{{3}}包含一个完整的示例。我只是拿起一个代码片段来说明await
方法中的用法:
put
...
final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition();