この単純なサンプルでデッドロックが発生する理由がわかりません。何が問題なのですか?
public static void main(String[] args) {
Object data = null;
new Thread(new Producer(data)).start();
new Thread(new Consumer(data)).start();
}
}
class Producer implements Runnable {
private Object data;
public Producer(Object data) {
this.data = data;
}
@Override
public void run() {
while (true) {
while (data != null) {}
data = new Object();
System.out.println("put");
}
}
}
class Consumer implements Runnable {
private Object data;
public Consumer(Object data) {
this.data = data;
}
@Override
public void run() {
while (true) {
while (data == null) { }
data = null;
System.out.println("get");
}
}