代码下方 - 没有wait()和notify()方法:
package com.jay;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Message message = new Message();
(new Thread(new Writer(message))).start();
(new Thread(new Reader(message))).start();
}
}
class Message {
private String message;
private boolean empty = true;
public synchronized String read() {
while (empty) {
}
empty = true;
return message;
}
public synchronized void write(String message) {
while (!empty) {
}
empty = false;
this.message = message;
}
}
class Writer implements Runnable {
private Message message;
public Writer(Message message) {
this.message = message;
}
public void run() {
String[] messages = {
"Humpty Dumpty sat on a wall",
"Humpty Dumpty had a great fall",
"All the king's horses and all the king's men",
"Couldn't put Humpty together again"
};
Random random = new Random();
for(int i=0; i<messages.length; i++) {
message.write(messages[i]);
try {
Thread.sleep(random.nextInt(2000));
} catch (InterruptedException e) {
// TODO: handle exception
}
}
message.write("Finished");
}
}
class Reader implements Runnable {
private Message message;
public Reader(Message message) {
this.message = message;
}
public void run() {
Random random = new Random();
for(String latestMessage = message.read(); !latestMessage.equals("Finished");
latestMessage = message.read()) {
System.out.println(latestMessage);
try {
Thread.sleep(random.nextInt(2000));
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
}
它在控制台下方打印 - 大部分时间都挂起:
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
有时会打印所有四条消息。
我的问题是:当Writer线程已经获得锁并且仍在循环时,Reader线程如何能够在read()方法中执行代码?
我很困惑。请帮我理解!
答案 0 :(得分:0)