JAVA多线程实现异步调用 在JAVA平台,实现异步调用的角色有如下三个角色:调用者、 提货单 、真实数据,一个调用者在调用耗时操作,不能立即返回数

    在JAVA平台,实现异步调用的角色有如下三个角色:调用者、 提货单 、真实数据,一个调用者在调用耗时操作,不能立即返回数

据时,先返回一个提货单,.然后在过一断时间后凭提货单来获取真正的数据.去蛋糕店买蛋糕,不需要等蛋糕做出来(假设现做要很长

时间),只需要领个提货单就可以了(去干别的事情),等到蛋糕做好了,再拿提货单取蛋糕就可以了。

  1. package com.somnus.async;
  2.  
  3. /**
  4. *
  5. * @Description: 顾客
  6. * @date 2016年3月9日 下午7:20:41
  7. * @version 1.0
  8. */
  9. public class Customer {
  10.  
  11. public static void main(String[] args) {
  12. System.out.println("main BEGIN");
  13. CakeShop host = new CakeShop();
  14. Cake cake1 = host.request(10, 'A');
  15. Cake cake2 = host.request(20, 'B');
  16. Cake cake3 = host.request(30, 'C');
  17. System.out.println("main otherJob BEGIN");
  18. try {
  19. Thread.sleep(2000);
  20. } catch (InterruptedException e) {
  21. }
  22.  
  23. System.out.println("main otherJob END");
  24. System.out.println("cake1 = " + cake1.getCake());
  25. System.out.println("cake2 = " + cake2.getCake());
  26. System.out.println("cake3 = " + cake3.getCake());
  27. System.out.println("main END");
  28.  
  29. }
  30. }

  这里的Customer类就相当于“顾客”,CakeShop就相当于“蛋糕店”,顾客向“蛋糕店”定蛋糕就相当于“发请求request”,返回的数据cakeDeliveryOrder的实例,就相当于提货单,而不是真正的“蛋糕”。在过一段时间后(sleep一段时间后),调用data1.getContent(),也就是拿提货单获取执行结果。

  下面来看一下,顾客定蛋糕后,蛋糕店做了什么:

  1. package com.somnus.async;
  2.  
  3. /**
  4. * @Description: TODO
  5. * @date 2016年3月9日 下午7:21:32
  6. * @version 1.0
  7. */
  8. public class CakeShop {
  9.  
  10. public Data request(final int count, final char c) {
  11.  
  12. System.out.println("request(" + count + ", " + c + ") BEGIN");
  13. // (1) 建立DeliveryOrder的实体
  14. final DeliveryOrder order = new DeliveryOrder();
  15.  
  16. // (2) 为了建立RealData的实体,启动新的线程
  17. new Thread() {
  18. public void run() {
  19. //在匿名内部类中使用count、order、c。
  20. CakeBaker cakeBaker = new CakeBaker(count, c);
  21. order.setCakeBaker(cakeBaker);
  22. }
  23. }.start();
  24. System.out.println("request(" + count + ", " + c + ") END");
  25.  
  26. // (3) 取回FutureData实体,作为传回值
  27. return order;
  28.  
  29. }
  30. }

  CakeShop(“蛋糕店”)在接到请求后,先生成了“提货单”DeliveryOrder的实例order,然后命令“蛋糕师傅CakeBaker去做蛋糕,CakeBaker相当于起个线程去做蛋糕了。然后host返回给顾客的仅仅是“提货单”future,而不是蛋糕。当蛋糕做好后,蛋糕师傅才能给对应的“提货单”蛋糕,也就是order.setCakeBaker(cakeBaker);。

  下面来看看蛋糕师傅是怎么做蛋糕的:

  建立一个字符串,包含count个c字符,为了表现出犯法需要花费一些时间,使用了sleep。

  1. package com.somnus.async;
  2.  
  3. public interface Cake {
  4.  
  5. /**
  6. * @return
  7. */
  8. String getCake();
  9.  
  10. }

  1. package com.somnus.async;
  2.  
  3. /**
  4. * @Description: 蛋糕师傅
  5. * @date 2016年3月9日 下午7:22:52
  6. * @version 1.0
  7. */
  8. public class CakeBaker implements Cake {
  9. private final String cake;
  10.  
  11. public CakeBaker(int count, char c) {
  12. System.out.println("making cake(" + count + ", " + c + ") BEGIN");
  13. char[] buffer = new char[count];
  14. for (int i = 0; i < count; i++) {
  15. buffer[i] = c;
  16. try {
  17. Thread.sleep(3000);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. System.out.println("making cake(" + count + ", " + c + ") END");
  23. this.cake = new String(buffer);
  24. }
  25.  
  26. public String getCake() {
  27. return cake;
  28. }
  29.  
  30. }


  现在来看看“提货单”order是怎么与蛋糕”cake”对应的:

  1. package com.somnus.async;
  2.  
  3. /**
  4. * @Description: 提货单
  5. * @date 2016年3月9日 下午7:25:06
  6. * @version 1.0
  7. */
  8. public class DeliveryOrder implements Cake {
  9. private CakeBaker cakeBaker = null;
  10.  
  11. private boolean ready = false;
  12.  
  13. public synchronized void setCakeBaker(CakeBaker cakeBaker) {
  14. if (ready) {
  15. return; // 防止setCakeBaker被调用两次以上。
  16. }
  17. this.cakeBaker = cakeBaker;
  18. this.ready = true;
  19. notifyAll();
  20. }
  21.  
  22. public synchronized String getCake() {
  23. while (!ready) {
  24. try {
  25. wait();
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. return cakeBaker.getCake();
  31. }
  32.  
  33. }


  顾客做完自己的事情后,会拿着自己的“提货单”来取蛋糕:

  1. System.out.println("cake1 = " + cake1.getCake());

  这时候如果蛋糕没做好,就只好等了:

  1. while (!ready) {
  2. try {
  3. wait();
  4. } catch (InterruptedException e) {
  5. e.printStackTrace();
  6. }
  7. }
  1. //等做好后才能取到
  2. return cakeBaker.getCake();



  程序分析

  对于每个请求,host都会生成一个线程,这个线程负责生成顾客需要的“蛋糕”。在等待一段时间以后,如果蛋糕还没有做好,顾客还必须等待。直到“蛋糕被做好”,也就是

  order.setCakeBaker(cakeBaker);执行以后,顾客才能拿走蛋糕。

  每个线程只是专门负责制作特定顾客所需要的“蛋糕”。也就是顾客A对应着蛋糕师傅A,顾客B对应着蛋糕师傅B。即使顾客B的蛋糕被先做好了,顾客A也只能等待蛋糕师傅A把蛋糕做好。换句话说,顾客之间没有竞争关系。

  类DeliveryOrder的两个方法被设置为synchronized,实际上蛋糕师傅A与顾客A之间的互斥关系,也就是顾客A必须等待蛋糕师傅A把蛋糕做好后,才能拿走,而与蛋糕师傅B是否做好了蛋糕没有关系。

据时,先返回一个提货单,.然后在过一断时间后凭提货单来获取真正的数据.去蛋糕店买蛋糕,不需要等蛋糕做出来(假设现做要很长

时间),只需要领个提货单就可以了(去干别的事情),等到蛋糕做好了,再拿提货单取蛋糕就可以了。

  1. package com.somnus.async;
  2.  
  3. /**
  4. *
  5. * @Description: 顾客
  6. * @date 2016年3月9日 下午7:20:41
  7. * @version 1.0
  8. */
  9. public class Customer {
  10.  
  11. public static void main(String[] args) {
  12. System.out.println("main BEGIN");
  13. CakeShop host = new CakeShop();
  14. Cake cake1 = host.request(10, 'A');
  15. Cake cake2 = host.request(20, 'B');
  16. Cake cake3 = host.request(30, 'C');
  17. System.out.println("main otherJob BEGIN");
  18. try {
  19. Thread.sleep(2000);
  20. } catch (InterruptedException e) {
  21. }
  22.  
  23. System.out.println("main otherJob END");
  24. System.out.println("cake1 = " + cake1.getCake());
  25. System.out.println("cake2 = " + cake2.getCake());
  26. System.out.println("cake3 = " + cake3.getCake());
  27. System.out.println("main END");
  28.  
  29. }
  30. }

  这里的Customer类就相当于“顾客”,CakeShop就相当于“蛋糕店”,顾客向“蛋糕店”定蛋糕就相当于“发请求request”,返回的数据cakeDeliveryOrder的实例,就相当于提货单,而不是真正的“蛋糕”。在过一段时间后(sleep一段时间后),调用data1.getContent(),也就是拿提货单获取执行结果。

  下面来看一下,顾客定蛋糕后,蛋糕店做了什么:

  1. package com.somnus.async;
  2.  
  3. /**
  4. * @Description: TODO
  5. * @date 2016年3月9日 下午7:21:32
  6. * @version 1.0
  7. */
  8. public class CakeShop {
  9.  
  10. public Data request(final int count, final char c) {
  11.  
  12. System.out.println("request(" + count + ", " + c + ") BEGIN");
  13. // (1) 建立DeliveryOrder的实体
  14. final DeliveryOrder order = new DeliveryOrder();
  15.  
  16. // (2) 为了建立RealData的实体,启动新的线程
  17. new Thread() {
  18. public void run() {
  19. //在匿名内部类中使用count、order、c。
  20. CakeBaker cakeBaker = new CakeBaker(count, c);
  21. order.setCakeBaker(cakeBaker);
  22. }
  23. }.start();
  24. System.out.println("request(" + count + ", " + c + ") END");
  25.  
  26. // (3) 取回FutureData实体,作为传回值
  27. return order;
  28.  
  29. }
  30. }

  CakeShop(“蛋糕店”)在接到请求后,先生成了“提货单”DeliveryOrder的实例order,然后命令“蛋糕师傅CakeBaker去做蛋糕,CakeBaker相当于起个线程去做蛋糕了。然后host返回给顾客的仅仅是“提货单”future,而不是蛋糕。当蛋糕做好后,蛋糕师傅才能给对应的“提货单”蛋糕,也就是order.setCakeBaker(cakeBaker);。

  下面来看看蛋糕师傅是怎么做蛋糕的:

  建立一个字符串,包含count个c字符,为了表现出犯法需要花费一些时间,使用了sleep。

  1. package com.somnus.async;
  2.  
  3. public interface Cake {
  4.  
  5. /**
  6. * @return
  7. */
  8. String getCake();
  9.  
  10. }

  1. package com.somnus.async;
  2.  
  3. /**
  4. * @Description: 蛋糕师傅
  5. * @date 2016年3月9日 下午7:22:52
  6. * @version 1.0
  7. */
  8. public class CakeBaker implements Cake {
  9. private final String cake;
  10.  
  11. public CakeBaker(int count, char c) {
  12. System.out.println("making cake(" + count + ", " + c + ") BEGIN");
  13. char[] buffer = new char[count];
  14. for (int i = 0; i < count; i++) {
  15. buffer[i] = c;
  16. try {
  17. Thread.sleep(3000);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. System.out.println("making cake(" + count + ", " + c + ") END");
  23. this.cake = new String(buffer);
  24. }
  25.  
  26. public String getCake() {
  27. return cake;
  28. }
  29.  
  30. }


  现在来看看“提货单”order是怎么与蛋糕”cake”对应的:

  1. package com.somnus.async;
  2.  
  3. /**
  4. * @Description: 提货单
  5. * @date 2016年3月9日 下午7:25:06
  6. * @version 1.0
  7. */
  8. public class DeliveryOrder implements Cake {
  9. private CakeBaker cakeBaker = null;
  10.  
  11. private boolean ready = false;
  12.  
  13. public synchronized void setCakeBaker(CakeBaker cakeBaker) {
  14. if (ready) {
  15. return; // 防止setCakeBaker被调用两次以上。
  16. }
  17. this.cakeBaker = cakeBaker;
  18. this.ready = true;
  19. notifyAll();
  20. }
  21.  
  22. public synchronized String getCake() {
  23. while (!ready) {
  24. try {
  25. wait();
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. return cakeBaker.getCake();
  31. }
  32.  
  33. }


  顾客做完自己的事情后,会拿着自己的“提货单”来取蛋糕:

  1. System.out.println("cake1 = " + cake1.getCake());

  这时候如果蛋糕没做好,就只好等了:

  1. while (!ready) {
  2. try {
  3. wait();
  4. } catch (InterruptedException e) {
  5. e.printStackTrace();
  6. }
  7. }
  1. //等做好后才能取到
  2. return cakeBaker.getCake();



  程序分析

  对于每个请求,host都会生成一个线程,这个线程负责生成顾客需要的“蛋糕”。在等待一段时间以后,如果蛋糕还没有做好,顾客还必须等待。直到“蛋糕被做好”,也就是

  order.setCakeBaker(cakeBaker);执行以后,顾客才能拿走蛋糕。

  每个线程只是专门负责制作特定顾客所需要的“蛋糕”。也就是顾客A对应着蛋糕师傅A,顾客B对应着蛋糕师傅B。即使顾客B的蛋糕被先做好了,顾客A也只能等待蛋糕师傅A把蛋糕做好。换句话说,顾客之间没有竞争关系。

  类DeliveryOrder的两个方法被设置为synchronized,实际上蛋糕师傅A与顾客A之间的互斥关系,也就是顾客A必须等待蛋糕师傅A把蛋糕做好后,才能拿走,而与蛋糕师傅B是否做好了蛋糕没有关系。

猜你喜欢

转载自blog.csdn.net/andyliulin/article/details/80887937