zoukankan      html  css  js  c++  java
  • Worker Thread等到工作来,来了就工作

    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();
            }
        }
    }
  • 相关阅读:
    Codeforces 1316B String Modification
    Codeforces 1305C Kuroni and Impossible Calculation
    Codeforces 1305B Kuroni and Simple Strings
    Codeforces 1321D Navigation System
    Codeforces 1321C Remove Adjacent
    Codeforces 1321B Journey Planning
    Operating systems Chapter 6
    Operating systems Chapter 5
    Abandoned country HDU
    Computer HDU
  • 原文地址:https://www.cnblogs.com/gaotianle/p/3308010.html
Copyright © 2011-2022 走看看