下面来看看蛋糕师傅是怎么做蛋糕的:
建立一个字符串,包含count个c字符,为了表现出犯法需要花费一些时间,使用了sleep。
public class RealData implements Data { private final String content;
public RealData(int count, char c) {
System.out.println("making RealData(" + count + ", " + c + ") BEGIN");
char[] buffer = new char[count];
for (int i = 0; i < count; i++) {
buffer[i] = c;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println("making RealData(" + count + ", " + c + ") END");
this.content = new String(buffer);
}
public String getContent() {
return content;
}
}
现在来看看“提货单”future是怎么与蛋糕"content"对应的:
public class FutureData implements Data { private RealData realdata = null;
private boolean ready = false;
public synchronized void setRealData(RealData realdata) {
if (ready) {
return; // 防止setRealData被调用两次以上。
}
this.realdata = realdata;
this.ready = true;
notifyAll();
}
public synchronized String getContent() {
while (!ready) {
try {
wait();
} catch (InterruptedException e) {
}
}
return realdata.getContent();
}
}
顾客做完自己的事情后,会拿着自己的“提货单”来取蛋糕:
System.out.println("data1 = " + data1.getContent());
这时候如果蛋糕没做好,就只好等了:
while (!ready) { try {
wait();
} catch (InterruptedException e) {
}
//等做好后才能取到
return realdata.getContent();
程序分析
对于每个请求,host都会生成一个线程,这个线程负责生成顾客需要的“蛋糕”。在等待一段时间以后,如果蛋糕还没有做好,顾客还必须等待。直到“蛋糕被做好”,也就是
future.setRealData(realdata); 执行以后,顾客才能拿走蛋糕。
每个线程只是专门负责制作特定顾客所需要的“蛋糕”。也就是顾客A对应着蛋糕师傅A,顾客B对应着蛋糕师傅B。即使顾客B的蛋糕被先做好了,顾客A也只能等待蛋糕师傅A把蛋糕做好。换句话说,顾客之间没有竞争关系。
类FutureData的两个方法被设置为synchronized,实际上蛋糕师傅A与顾客A之间的互斥关系,也就是顾客A必须等待蛋糕师傅A把蛋糕做好后,才能拿走,而与蛋糕师傅B是否做好了蛋糕没有关系。
建立一个字符串,包含count个c字符,为了表现出犯法需要花费一些时间,使用了sleep。
public class RealData implements Data { private final String content;
public RealData(int count, char c) {
System.out.println("making RealData(" + count + ", " + c + ") BEGIN");
char[] buffer = new char[count];
for (int i = 0; i < count; i++) {
buffer[i] = c;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println("making RealData(" + count + ", " + c + ") END");
this.content = new String(buffer);
}
public String getContent() {
return content;
}
}
现在来看看“提货单”future是怎么与蛋糕"content"对应的:
public class FutureData implements Data { private RealData realdata = null;
private boolean ready = false;
public synchronized void setRealData(RealData realdata) {
if (ready) {
return; // 防止setRealData被调用两次以上。
}
this.realdata = realdata;
this.ready = true;
notifyAll();
}
public synchronized String getContent() {
while (!ready) {
try {
wait();
} catch (InterruptedException e) {
}
}
return realdata.getContent();
}
}
顾客做完自己的事情后,会拿着自己的“提货单”来取蛋糕:
System.out.println("data1 = " + data1.getContent());
这时候如果蛋糕没做好,就只好等了:
while (!ready) { try {
wait();
} catch (InterruptedException e) {
}
//等做好后才能取到
return realdata.getContent();
程序分析
对于每个请求,host都会生成一个线程,这个线程负责生成顾客需要的“蛋糕”。在等待一段时间以后,如果蛋糕还没有做好,顾客还必须等待。直到“蛋糕被做好”,也就是
future.setRealData(realdata); 执行以后,顾客才能拿走蛋糕。
每个线程只是专门负责制作特定顾客所需要的“蛋糕”。也就是顾客A对应着蛋糕师傅A,顾客B对应着蛋糕师傅B。即使顾客B的蛋糕被先做好了,顾客A也只能等待蛋糕师傅A把蛋糕做好。换句话说,顾客之间没有竞争关系。
类FutureData的两个方法被设置为synchronized,实际上蛋糕师傅A与顾客A之间的互斥关系,也就是顾客A必须等待蛋糕师傅A把蛋糕做好后,才能拿走,而与蛋糕师傅B是否做好了蛋糕没有关系。