class Queue {
private int messagein = 0; // メッセージがない場合は0、ある場合は1
int start = 0;
public Queue(int size) {
}
while (messagein >= buf.length) { // キューが満杯の場合、待機
wait();
}
int end = (start + messagein) % buf.length; // 書き込み位置を計算
buf[end] = str;
messagein++; // メッセージ数を増加
notifyAll();
}
while (messagein == 0) { // キューが空の場合、待機
wait();
}
String mes
= buf
[start
]; // メッセージを取得 start = (start + 1) % buf.length; // 次の読み取り位置を計算
messagein--; // メッセージ数を減少
notifyAll();
return mes;
}
}
Queue send_q, recv_q;
Asan(Queue send, Queue recv) {
send_q = send;
recv_q = recv;
}
public void run() {
try {
// メッセージを送信
send_q.wrtMessage("Bさん,こんにちは.Aより");
send_q.wrtMessage("おなかすいた");
send_q.wrtMessage("カレー食べたい");
send_q.wrtMessage("Bye"); // 終了メッセージを送信
// メッセージを受信
while (!(retmes = recv_q.readMessage()).equals("Bye")) {
System.
out.
println("Asan: Bsanから受信 -> 『" + retmes
+ "』"); }
System.
out.
println("Asan: 終了"); System.
err.
println(e.
getMessage()); }
}
}
Queue send_q, recv_q;
Bsan(Queue send, Queue recv) {
send_q = send;
recv_q = recv;
}
public void run() {
try {
// メッセージを受信
while (!(retmes = recv_q.readMessage()).equals("Bye")) {
System.
out.
println("Bsan: Asanから受信 -> 『" + retmes
+ "』");
// 受け取ったメッセージに応じて返信
send_q.wrtMessage("ハヤシライス食べたい");
send_q.wrtMessage("カレーもいいね!");
send_q.wrtMessage("Bye");
}
// "Bye"を受け取ったら終了
send_q.wrtMessage("Bye"); // 終了メッセージを送信
System.
out.
println("Bsan: 終了"); System.
err.
println(e.
getMessage()); }
}
}
class ideone {
public static void main
(String[] args
) { Queue obj1 = new Queue(10); // Asan -> Bsan のキュー
Queue obj2 = new Queue(10); // Bsan -> Asan のキュー
Asan sy1 = new Asan(obj1, obj2); // Aさんのインスタンス
Bsan sy2 = new Bsan(obj2, obj1); // Bさんのインスタンス
th1.start(); // スレッドを開始
th2.start();
}
}
Y2xhc3MgUXVldWUgewogICA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gKyAi44CPIik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJBc2FuOiDntYLkuoYiKTsKICAgICAgICB9IGNhdGNoIChFeGNlcHRpb24gZSkgewogICAgICAgICAgICBTeXN0ZW0uZXJyLnByaW50bG4oZS5nZXRNZXNzYWdlKCkpOwogICAgICAgIH0KICAgIH0KfQoKY2xhc3MgQnNhbiBpbXBsZW1lbnRzIFJ1bm5hYmxlIHsKICAgIFF1ZXVlIHNlbmRfcSwgcmVjdl9xOwoKICAgIEJzYW4oUXVldWUgc2VuZCwgUXVldWUgcmVjdikgewogICAgICAgIHNlbmRfcSA9IHNlbmQ7CiAgICAgICAgcmVjdl9xID0gcmVjdjsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBydW4oKSB7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgU3RyaW5nIHJldG1lczsKCiAgICAgICAgICAgIC8vIOODoeODg+OCu+ODvOOCuOOCkuWPl+S/oQogICAgICAgICAgICB3aGlsZSAoIShyZXRtZXMgPSByZWN2X3EucmVhZE1lc3NhZ2UoKSkuZXF1YWxzKCJCeWUiKSkgewogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJCc2FuOiBBc2Fu44GL44KJ5Y+X5L+hIC0+IOOAjiIgKyByZXRtZXMgKyAi44CPIik7CgogICAgICAgICAgICAgICAgLy8g5Y+X44GR5Y+W44Gj44Gf44Oh44OD44K744O844K444Gr5b+c44GY44Gm6L+U5L+hCiAgICAgICAgICAgICAgICAgICAgc2VuZF9xLndydE1lc3NhZ2UoIuODj+ODpOOCt+ODqeOCpOOCuemjn+OBueOBn+OBhCIpOwogICAgICAgICAgICAgICAgICAgIHNlbmRfcS53cnRNZXNzYWdlKCLjgqvjg6zjg7zjgoLjgYTjgYTjga3vvIEiKTsKICAgICAgICAgICAgICAgICAgICBzZW5kX3Eud3J0TWVzc2FnZSgiQnllIik7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgICAgICAgICAvLyAiQnllIuOCkuWPl+OBkeWPluOBo+OBn+OCiee1guS6hgogICAgICAgICAgICBzZW5kX3Eud3J0TWVzc2FnZSgiQnllIik7IC8vIOe1guS6huODoeODg+OCu+ODvOOCuOOCkumAgeS/oQogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkJzYW46IOe1guS6hiIpOwogICAgICAgIH0gY2F0Y2ggKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgIFN5c3RlbS5lcnIucHJpbnRsbihlLmdldE1lc3NhZ2UoKSk7CiAgICAgICAgfQogICAgfQp9CgpjbGFzcyBpZGVvbmUgewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFF1ZXVlIG9iajEgPSBuZXcgUXVldWUoMTApOyAvLyBBc2FuIC0+IEJzYW4g44Gu44Kt44Ol44O8CiAgICAgICAgUXVldWUgb2JqMiA9IG5ldyBRdWV1ZSgxMCk7IC8vIEJzYW4gLT4gQXNhbiDjga7jgq3jg6Xjg7wKCiAgICAgICAgQXNhbiBzeTEgPSBuZXcgQXNhbihvYmoxLCBvYmoyKTsgLy8gQeOBleOCk+OBruOCpOODs+OCueOCv+ODs+OCuQogICAgICAgIEJzYW4gc3kyID0gbmV3IEJzYW4ob2JqMiwgb2JqMSk7IC8vIELjgZXjgpPjga7jgqTjg7Pjgrnjgr/jg7PjgrkKCiAgICAgICAgVGhyZWFkIHRoMSA9IG5ldyBUaHJlYWQoc3kxKTsgLy8gQeOBleOCk+OBruOCueODrOODg+ODiQogICAgICAgIFRocmVhZCB0aDIgPSBuZXcgVGhyZWFkKHN5Mik7IC8vIELjgZXjgpPjga7jgrnjg6zjg4Pjg4kKCiAgICAgICAgdGgxLnN0YXJ0KCk7IC8vIOOCueODrOODg+ODieOCkumWi+WniwogICAgICAgIHRoMi5zdGFydCgpOwogICAgfQp9Cg==