Pattern參與者 :
* GuardedObject (被守護的對象) :
使用範例 :
* Request 類 :
Request 類是用來表示請求. 雖然說是請求, 但這裡不過是用來作為ClientThread 傳遞給 ServerThread 的實例, 所以沒有特殊處理. Request 只是一個存放名稱 (name 字段) 的類而已.
代碼 :
- package dp.thread.ch3;
- public class Request {
- private final String name;
- public Request(String n){
- this.name = n;
- }
- public String getName() {
- return name;
- }
- public String toString(){
- return "[ Request "+name+" ]";
- }
- }
* RequestQueue 類 :
RequestQueue 類是用來依次存放請求的類. 這個類擁有 getRequest 與 putRequest 兩個方法 :
> getRequest 方法
> putRequest 方法
這裡要注意的地方有 :
代碼 :
- package dp.thread.ch3;
- import java.util.*;
- public class RequestQueue {
- private final LinkedList
queue = new LinkedList(); - public synchronized Request getRequest(){
- while(queue.size() <= 0){
- try{
- wait();
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- return (Request)queue.removeFirst();
- }
- public synchronized void putRequest(Request r){
- queue.addLast(r);
- notifyAll();
- }
- }
* ClientThread 類 :
ClientThread 類是用來模擬送出請求的線程. ClientThread 擁有 RequestQueue 的物件, 會不斷調用 putRequest.
代碼 :
- package dp.thread.ch3;
- import java.util.Random;
- public class ClientThread extends Thread{
- private Random random;
- private RequestQueue requestQueue;
- private int taskCount = 10000;
- public ClientThread(RequestQueue rq, String name, long seed,int tc){
- super(name);
- this.requestQueue = rq;
- this.random = new Random(seed);
- if(tc>0){
- taskCount = tc;
- }
- }
- public void run(){
- for(int i=0;i
- Request request = new Request("No."+i);
- System.out.println(Thread.currentThread().getName()+" requests "+request);
- requestQueue.putRequest(request);
- try{
- Thread.sleep(random.nextInt(1000));
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
* ServerThread 類 :
ServerThread 類是用來接受請求的線程, 這個類也擁有 RequestQueue 的物件 (通常與 ClientThread 會是同一個) . ServerThread 會調用 getRequest 方法獲取請求.
代碼 :
- package dp.thread.ch3;
- import java.util.Random;
- public class ServerThread extends Thread{
- private Random random;
- private RequestQueue requestQueue;
- private int taskCount = 10000;
- public ServerThread(RequestQueue rq, String name, long seed,int tc){
- super(name);
- this.requestQueue = rq;
- this.random = new Random(seed);
- if(tc>0){
- taskCount = tc;
- }
- }
- public void run(){
- for(int i=0;i < taskCount;i++){
- Request request = requestQueue.getRequest();
- System.out.println(Thread.currentThread().getName()+" handles "+request);
- try{
- Thread.sleep(random.nextInt(1000));
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
* Main 類 :
在 Main 類中, 首先會先建立 RequestQueue 的實例, 並建立名為 Peter 的 ClientThread 與 名為 Mary 的 ServerThread, 並啟動它們.
代碼 :
- package dp.thread.ch3;
- public class Main {
- public static void main(String[] args) {
- int taskCount = 50;
- RequestQueue requestQueue = new RequestQueue();
- new ClientThread(requestQueue,"Peter",31252L,taskCount).start();
- new ServerThread(requestQueue,"Mary",245191L,taskCount).start();
- }
- }
執行結果 :
沒有留言:
張貼留言