0
点赞
收藏
分享

微信扫一扫

java worker thread模式


 

 

Worker Thread Pattern的参与者:

1. Client(委托人)参与者

    Client参与者会建立Request参与者,并传给Channel参与者。

2. Channel(通路)参与者

    Channel参与者会从Client参与者获取Request参与者,传递给Worker参与者。

3. Worker(工人)参与者

    Worker参与者会从Channel参与者获取Request参与者,并执行这份工作。当工作结束之后,会去拿下一个Request参与者。

4. Request(请求)参与者

    Reqeust参与者用来表示工作。Request参与者会存放执行这份工作所需要的数据。

---------------------------------------------

控制承载量:

Worker参与者的数量是可以自由设置的。提高Worker参与者的数量,可以提高并发处理的工作量。但是如果准备的Worker参与者数量比同时间的工作量来的多,有些Worker参与者也派不上用场,没活干,还会占用内存。所以有必要配合实际软件使用的需要来调整Worker参与者的数量。

---------------------------------------------

Invocation与execution的分离:

Client参与者会发出工作请求,工作的内容以Request的形式包装起来,传递个Channel参与者。这个过程,对应于普通的方法调用来说,就相当于“评估自变量,启动方法”的操作。

另一方面,Worker参与者会使用从Channel参与者拿来的Request参与者,进行实际的操作。这个过程,对应于普通的方法调用来说,可以对应到“执行方法”的部分。

普通的方法调用的操作,“启动方法”和“执行方法”是连续进行的。当一个方法一经调用,就会马上继续执行,启动和执行是密不可分的。

在Worker Thread Pattern中,实现了“启动方法”和“执行方法”的分离。这样的好处有:

1. 提高响应性。

invocation可以继续自己前进,不用等待execution,这样能提高程序的响应性。

2. 控制实行顺序。

invocation和execution分离,execute的顺序就可以与invoke的次序无关,也就是说,我们可以对Request参与者设立优先级,控制Channel参与者传递Request给Worker参与者的顺序。

3. 可以取消和重复执行。

如果能将invocation和execution分离,那么就能做到在execute之前,把已经invoke的请求的execution取消掉。

同样,只要把Request参与者保存下来,就可以重复执行。

4. 分散处理的第一步

因为invocation和execution分离了,所以invoke和execute的操作也比较容易拆开在不同的计算机上面执行,相当于Reqeust参与者的对象,可以通过网络传送的另外一台计算机上。

------------------------------------------------

多态的Request参与者:

因为WorkerThread参与者只不过是单纯的接收Request的实例,调用Request的execute方法,

那么当我们建立Request类的子类,并将其传递给Channel,WorkerThread也应该能够正确的处理。

执行工作所需要的所有信息,都定义在Request参与者里。所以即使建立出多态的Request参与者,增加工作的种类,Channel参与者和Worker参与者都不需要进行任何修改。

------------------------------------------

实例:


1. public class
2. private final String name; //  委托者
3. private final int number;  // 请求编号
4. private static final Random random = new
5. public Request(String name, int
6. this.name = name;  
7. this.number = number;  
8.     }  
9. public void
10. " executes " + this);  
11. try
12. 1000));  
13. catch
14.         }  
15.     }  
16. public
17. return "[ Request from " + name + " No." + number + " ]";  
18.     }  
19. }

1. public class ClientThread extends
2. private final
3. private static final Random random = new
4. public
5. super(name);  
6. this.channel = channel;  
7.     }  
8. public void
9. try
10. for (int i = 0; true; i++) {  
11. new
12.                 channel.putRequest(request);  
13. 1000));  
14.             }  
15. catch
16.         }  
17.     }  
18. }



1. public class WorkerThread extends
2. private final
3. public
4. super(name);  
5. this.channel = channel;  
6.     }  
7. public void
8. while (true) {  
9.             Request request = channel.takeRequest();  
10.             request.execute();  
11.         }  
12.     }  
13. }


1. public class
2. private static final int MAX_REQUEST = 100;  
3. private final
4. private int tail;  // 下一个putRequest的地方
5. private int head;  // 下一个takeRequest的地方
6. private int count; // Request的数量
7.   
8. private final
9.   
10. public Channel(int
11. this.requestQueue = new
12. this.head = 0;  
13. this.tail = 0;  
14. this.count = 0;  
15.   
16. new
17. for (int i = 0; i < threadPool.length; i++) {  
18. new WorkerThread("Worker-" + i, this);  
19.         }  
20.     }  
21. public void
22. for (int i = 0; i < threadPool.length; i++) {  
23.             threadPool[i].start();  
24.         }  
25.     }  
26. public synchronized void
27. while
28. try
29.                 wait();  
30. catch
31.             }  
32.         }  
33.         requestQueue[tail] = request;  
34. 1) % requestQueue.length;  
35.         count++;  
36.         notifyAll();  
37.     }  
38. public synchronized
39. while (count <= 0) {  
40. try
41.                 wait();  
42. catch
43.             }  
44.         }  
45.         Request request = requestQueue[head];  
46. 1) % requestQueue.length;  
47.         count--;  
48.         notifyAll();  
49. return
50.     }  
51. }

 


1. public class
2. public static void
3. new Channel(5);   // 工人线程的數量
4.         channel.startWorkers();  
5. new ClientThread("Alice", channel).start();  
6. new ClientThread("Bobby", channel).start();  
7. new ClientThread("Chris", channel).start();  
8.     }  
9. }

举报

相关推荐

0 条评论