当前位置: 技术问答>java相关
关于线程的综合使用问题(希望你能帮助我哦^^)
来源: 互联网 发布时间:2017-03-23
本文导语: 为什么结果会是这样呢 class Mailbox { public boolean request; public String message; public synchronized void store(String mess) { while (request==true) { try {System.out.println("i am waiting in store");wait();} catch (InterruptedException e) {} } requ...
为什么结果会是这样呢
class Mailbox {
public boolean request;
public String message;
public synchronized void store(String mess) {
while (request==true) {
try {System.out.println("i am waiting in store");wait();}
catch (InterruptedException e)
{}
}
request=true;message=mess;notify();
}
public synchronized String retrieve() {
while (request==false) {
try {System.out.println("i am waiting in retrieve");wait();}
catch (InterruptedException e) {}
}
request=false;notify();return message;
}
}
class MyThread extends Thread {
Mailbox box;
MyThread(Mailbox box){this.box=box;}
public void run() {
int i;
while(true) {
for (i=0;i
class Mailbox {
public boolean request;
public String message;
public synchronized void store(String mess) {
while (request==true) {
try {System.out.println("i am waiting in store");wait();}
catch (InterruptedException e)
{}
}
request=true;message=mess;notify();
}
public synchronized String retrieve() {
while (request==false) {
try {System.out.println("i am waiting in retrieve");wait();}
catch (InterruptedException e) {}
}
request=false;notify();return message;
}
}
class MyThread extends Thread {
Mailbox box;
MyThread(Mailbox box){this.box=box;}
public void run() {
int i;
while(true) {
for (i=0;i