在JAVA平台,实现异步调用的角色有如下三个角色:调用者、 提货单 、真实数据,一个调用者在调用耗时操作,不能立即返回数
据时,先返回一个提货单,.然后在过一断时间后凭提货单来获取真正的数据.去蛋糕店买蛋糕,不需要等蛋糕做出来(假设现做要很长
时间),只需要领个提货单就可以了(去干别的事情),等到蛋糕做好了,再拿提货单取蛋糕就可以了。
package com.somnus.async;
/**
*
* @Description: 顾客
- * @date 2016年3月9日 下午7:20:41
* @version 1.0
*/
public class Customer {
public static void main(String[] args) {
System.out.println("main BEGIN");
CakeShop host = new CakeShop();
Cake cake1 = host.request(10, 'A');
Cake cake2 = host.request(20, 'B');
Cake cake3 = host.request(30, 'C');
System.out.println("main otherJob BEGIN");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("main otherJob END");
System.out.println("cake1 = " + cake1.getCake());
System.out.println("cake2 = " + cake2.getCake());
System.out.println("cake3 = " + cake3.getCake());
System.out.println("main END");
}
}
这里的Customer
类就相当于“顾客”,CakeShop
就相当于“蛋糕店”,顾客向“蛋糕店”定蛋糕就相当于“发请求request”,返回的数据cake
是DeliveryOrder
的实例,就相当于提货单,而不是真正的“蛋糕”。在过一段时间后(sleep一段时间后),调用data1.getContent()
,也就是拿提货单获取执行结果。
下面来看一下,顾客定蛋糕后,蛋糕店做了什么:
- package com.somnus.async;
- /**
- * @Description: TODO
- * @date 2016年3月9日 下午7:21:32
- * @version 1.0
- */
- public class CakeShop {
- public Data request(final int count, final char c) {
- System.out.println("request(" + count + ", " + c + ") BEGIN");
- // (1) 建立DeliveryOrder的实体
- final DeliveryOrder order = new DeliveryOrder();
- // (2) 为了建立RealData的实体,启动新的线程
- new Thread() {
- public void run() {
- //在匿名内部类中使用count、order、c。
- CakeBaker cakeBaker = new CakeBaker(count, c);
- order.setCakeBaker(cakeBaker);
- }
- }.start();
- System.out.println("request(" + count + ", " + c + ") END");
- // (3) 取回FutureData实体,作为传回值
- return order;
- }
- }
CakeShop
(“蛋糕店”)在接到请求后,先生成了“提货单”DeliveryOrder
的实例order
,然后命令“蛋糕师傅CakeBaker
去做蛋糕,CakeBaker
相当于起个线程去做蛋糕了。然后host返回给顾客的仅仅是“提货单”future,而不是蛋糕。当蛋糕做好后,蛋糕师傅才能给对应的“提货单”蛋糕,也就是order.setCakeBaker(cakeBaker)
;。
下面来看看蛋糕师傅是怎么做蛋糕的:
建立一个字符串,包含count个c字符,为了表现出犯法需要花费一些时间,使用了sleep。
package com.somnus.async;
public interface Cake {
/**
* @return
*/
String getCake();
}
- package com.somnus.async;
- /**
- * @Description: 蛋糕师傅
- * @date 2016年3月9日 下午7:22:52
- * @version 1.0
- */
- public class CakeBaker implements Cake {
- private final String cake;
- public CakeBaker(int count, char c) {
- System.out.println("making cake(" + count + ", " + c + ") BEGIN");
- char[] buffer = new char[count];
- for (int i = 0; i < count; i++) {
- buffer[i] = c;
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println("making cake(" + count + ", " + c + ") END");
- this.cake = new String(buffer);
- }
- public String getCake() {
- return cake;
- }
- }
现在来看看“提货单”order是怎么与蛋糕”cake”对应的:
package com.somnus.async;
/**
* @Description: 提货单
* @date 2016年3月9日 下午7:25:06
* @version 1.0
*/
public class DeliveryOrder implements Cake {
private CakeBaker cakeBaker = null;
private boolean ready = false;
public synchronized void setCakeBaker(CakeBaker cakeBaker) {
if (ready) {
return; // 防止setCakeBaker被调用两次以上。
}
this.cakeBaker = cakeBaker;
this.ready = true;
notifyAll();
}
public synchronized String getCake() {
while (!ready) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return cakeBaker.getCake();
}
}
顾客做完自己的事情后,会拿着自己的“提货单”来取蛋糕:
- System.out.println("cake1 = " + cake1.getCake());
这时候如果蛋糕没做好,就只好等了:
while (!ready) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- //等做好后才能取到
- return cakeBaker.getCake();
程序分析
对于每个请求,host都会生成一个线程,这个线程负责生成顾客需要的“蛋糕”。在等待一段时间以后,如果蛋糕还没有做好,顾客还必须等待。直到“蛋糕被做好”,也就是
order.setCakeBaker(cakeBaker);
执行以后,顾客才能拿走蛋糕。
每个线程只是专门负责制作特定顾客所需要的“蛋糕”。也就是顾客A对应着蛋糕师傅A,顾客B对应着蛋糕师傅B。即使顾客B的蛋糕被先做好了,顾客A也只能等待蛋糕师傅A把蛋糕做好。换句话说,顾客之间没有竞争关系。
类DeliveryOrder
的两个方法被设置为synchronized
,实际上蛋糕师傅A与顾客A之间的互斥关系,也就是顾客A必须等待蛋糕师傅A把蛋糕做好后,才能拿走,而与蛋糕师傅B是否做好了蛋糕没有关系。
据时,先返回一个提货单,.然后在过一断时间后凭提货单来获取真正的数据.去蛋糕店买蛋糕,不需要等蛋糕做出来(假设现做要很长
时间),只需要领个提货单就可以了(去干别的事情),等到蛋糕做好了,再拿提货单取蛋糕就可以了。
package com.somnus.async;
/**
*
* @Description: 顾客
- * @date 2016年3月9日 下午7:20:41
* @version 1.0
*/
public class Customer {
public static void main(String[] args) {
System.out.println("main BEGIN");
CakeShop host = new CakeShop();
Cake cake1 = host.request(10, 'A');
Cake cake2 = host.request(20, 'B');
Cake cake3 = host.request(30, 'C');
System.out.println("main otherJob BEGIN");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("main otherJob END");
System.out.println("cake1 = " + cake1.getCake());
System.out.println("cake2 = " + cake2.getCake());
System.out.println("cake3 = " + cake3.getCake());
System.out.println("main END");
}
}
这里的Customer
类就相当于“顾客”,CakeShop
就相当于“蛋糕店”,顾客向“蛋糕店”定蛋糕就相当于“发请求request”,返回的数据cake
是DeliveryOrder
的实例,就相当于提货单,而不是真正的“蛋糕”。在过一段时间后(sleep一段时间后),调用data1.getContent()
,也就是拿提货单获取执行结果。
下面来看一下,顾客定蛋糕后,蛋糕店做了什么:
- package com.somnus.async;
- /**
- * @Description: TODO
- * @date 2016年3月9日 下午7:21:32
- * @version 1.0
- */
- public class CakeShop {
- public Data request(final int count, final char c) {
- System.out.println("request(" + count + ", " + c + ") BEGIN");
- // (1) 建立DeliveryOrder的实体
- final DeliveryOrder order = new DeliveryOrder();
- // (2) 为了建立RealData的实体,启动新的线程
- new Thread() {
- public void run() {
- //在匿名内部类中使用count、order、c。
- CakeBaker cakeBaker = new CakeBaker(count, c);
- order.setCakeBaker(cakeBaker);
- }
- }.start();
- System.out.println("request(" + count + ", " + c + ") END");
- // (3) 取回FutureData实体,作为传回值
- return order;
- }
- }
CakeShop
(“蛋糕店”)在接到请求后,先生成了“提货单”DeliveryOrder
的实例order
,然后命令“蛋糕师傅CakeBaker
去做蛋糕,CakeBaker
相当于起个线程去做蛋糕了。然后host返回给顾客的仅仅是“提货单”future,而不是蛋糕。当蛋糕做好后,蛋糕师傅才能给对应的“提货单”蛋糕,也就是order.setCakeBaker(cakeBaker)
;。
下面来看看蛋糕师傅是怎么做蛋糕的:
建立一个字符串,包含count个c字符,为了表现出犯法需要花费一些时间,使用了sleep。
package com.somnus.async;
public interface Cake {
/**
* @return
*/
String getCake();
}
- package com.somnus.async;
- /**
- * @Description: 蛋糕师傅
- * @date 2016年3月9日 下午7:22:52
- * @version 1.0
- */
- public class CakeBaker implements Cake {
- private final String cake;
- public CakeBaker(int count, char c) {
- System.out.println("making cake(" + count + ", " + c + ") BEGIN");
- char[] buffer = new char[count];
- for (int i = 0; i < count; i++) {
- buffer[i] = c;
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println("making cake(" + count + ", " + c + ") END");
- this.cake = new String(buffer);
- }
- public String getCake() {
- return cake;
- }
- }
现在来看看“提货单”order是怎么与蛋糕”cake”对应的:
package com.somnus.async;
/**
* @Description: 提货单
* @date 2016年3月9日 下午7:25:06
* @version 1.0
*/
public class DeliveryOrder implements Cake {
private CakeBaker cakeBaker = null;
private boolean ready = false;
public synchronized void setCakeBaker(CakeBaker cakeBaker) {
if (ready) {
return; // 防止setCakeBaker被调用两次以上。
}
this.cakeBaker = cakeBaker;
this.ready = true;
notifyAll();
}
public synchronized String getCake() {
while (!ready) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return cakeBaker.getCake();
}
}
顾客做完自己的事情后,会拿着自己的“提货单”来取蛋糕:
- System.out.println("cake1 = " + cake1.getCake());
这时候如果蛋糕没做好,就只好等了:
while (!ready) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- //等做好后才能取到
- return cakeBaker.getCake();
程序分析
对于每个请求,host都会生成一个线程,这个线程负责生成顾客需要的“蛋糕”。在等待一段时间以后,如果蛋糕还没有做好,顾客还必须等待。直到“蛋糕被做好”,也就是
order.setCakeBaker(cakeBaker);
执行以后,顾客才能拿走蛋糕。
每个线程只是专门负责制作特定顾客所需要的“蛋糕”。也就是顾客A对应着蛋糕师傅A,顾客B对应着蛋糕师傅B。即使顾客B的蛋糕被先做好了,顾客A也只能等待蛋糕师傅A把蛋糕做好。换句话说,顾客之间没有竞争关系。
类DeliveryOrder
的两个方法被设置为synchronized
,实际上蛋糕师傅A与顾客A之间的互斥关系,也就是顾客A必须等待蛋糕师傅A把蛋糕做好后,才能拿走,而与蛋糕师傅B是否做好了蛋糕没有关系。