高天乐 welcome you

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来。

Worker Thread也叫做background thread,另外,也有人把视点放在管理工人线程的地方,称之为Thread Pool。

 

public class WorkerThreadTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Channel channel = new Channel(5);
        channel.startWorkers();
        new WorkerClientThread("Alice", channel).start();
        new WorkerClientThread("Bobby", channel).start();
        new WorkerClientThread("Chris", channel).start();
    }

}

class WorkerClientThread extends Thread{
    private final Channel channel;
    private static final Random random = new Random();
    public WorkerClientThread(String name,Channel channel){
        super(name);
        
        this.channel=channel;
    }
    @Override
    public void run() {
        try{
            for(int i=0;true; i++){
                WorkerRequest request = new WorkerRequest(getName(),i);
                channel.put(request);
                Thread.sleep(random.nextInt(1000));
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

class WorkerRequest{
    private final String name;
    private final int number;
    private static final Random random = new Random();
    
    public WorkerRequest(String name,int number){
        this.name=name;
        this.number=number;
    }
    
    public void execute(){
        System.out.println(Thread.currentThread().getName() + " executes " + this);
        
        try {
            Thread.sleep(random.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "[Request from " + name + " No." + number + "]";
    }
}

class Channel{
    private static final int MAX_REQUEST=100;
    private final WorkerRequest[] requestQueue;
    private int tail;
    private int head;
    private int count;
    
    private final WorkerThread[] threadPool;
    
    public Channel(int threads){
        this.requestQueue=new WorkerRequest[MAX_REQUEST];
        this.head = 0;
        this.tail=0;
        this.count=0;
        
        threadPool=new WorkerThread[threads];
        
        for(int i=0;i < threadPool.length;i++){
            threadPool[i] = new WorkerThread("Worker-" + i, this);
        }
    }
    
    public void startWorkers(){
        for(int i=0;i<threadPool.length;i++){
            threadPool[i].start();
        }
    }
    
    public synchronized void put(WorkerRequest request){
        while(count>=requestQueue.length){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        requestQueue[tail]=request;
        tail=(tail+1)%requestQueue.length;
        count++;
        notify();
    }
    
    public synchronized WorkerRequest take(){
        while(count<=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        WorkerRequest request = requestQueue[head];
        
        head=(head+1)%requestQueue.length;
        count--;
        notify();
        return request;
    }
}

class WorkerThread extends Thread{
    private final Channel channel;
    public WorkerThread(String name, Channel channel){
        super(name);
        this.channel=channel;
    }
    @Override
    public void run() {
        while(true){
            WorkerRequest request = channel.take();
            request.execute();
        }
    }
}

 

posted on 2013-09-08 00:38  高天乐  阅读(1718)  评论(0编辑  收藏  举报