📄 mediator.java~9~
字号:
package threadmediator;
public class Mediator {
private int number;//存储槽
private boolean slotFull = false;
public int count;//计数器
void Mediator()
{
count=1;
}
//同步化,避免两个线程同时执行
public synchronized void storeMessage (int num) {
while (slotFull == true)//存储槽没有空间了
try {
wait ();
}
catch (InterruptedException e) {}
slotFull = true;
number = num;
count++;
if(count%5==0)System.out.println("");
notifyAll ();
}
public synchronized int retrieveMessage () {
while (slotFull == false) //存储槽空信息
try {
wait ();
}
catch (InterruptedException e) {}
slotFull = false;
notifyAll ();
count++;
if(count%5==0)System.out.println("");
return number;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -