`

简单的模拟线程池

 
阅读更多

package com.threadPool;

import java.util.LinkedList;
import java.util.logging.Logger;

/*start函数是任务的生产者,不断地向队列中添加任务;
 * 2个工作者线程worker1和worker2,他们是工作者线程,队列中如果有任务,就领取任务执行,若无任务就休眠*/

public class TestPool {
	private final static int  nThreads = 100;
    private final static Mythread[] threads = new Mythread[nThreads];
    private final static LinkedList queue = new LinkedList();
    public TestPool(int nThreads)
    {
        PoolWorker Worker1 = new PoolWorker();
        Worker1.setName("poolwork1");
        Worker1.start();
        PoolWorker Worker2 = new PoolWorker();
        Worker2.setName("poolwork2");
        Worker2.start();
        
        for (int i=0; i<nThreads; i++) {
            threads[i] = new Mythread();
            threads[i].setName("test "+i);
//            execute(threads[i]);
            /*try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
            
//            threads[i].start();
        }
    }
    
    private static void start(){
    int i=0;
    while (true) {
    	execute(threads[i%100]);
    	i = i+1;
		/*try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
    }
    public static void execute(Runnable r) {
        synchronized(queue) {
            queue.addLast(r);
            queue.notify();
        }
    }
    private class PoolWorker extends Thread {
        public void run() {
            Runnable r;
            while (true) {
                synchronized(queue) {
                    while (queue.isEmpty()) {
                        try
                        {
                        	System.out.print(getName()+" wait \n");
                            queue.wait();//会被queue.notify()唤醒,重新从run开始执行,而sleep不同,sleep醒之后会接着向下执行。
                            System.out.print(getName()+" wakeup \n");
                        }
                        catch (InterruptedException ignored)
                        {
                        }
                    }
                    r = (Runnable) queue.removeFirst();
                }
                // If we don't catch RuntimeException, 
                // the pool could leak threads
                try {
                	System.out.print(getName()+" and task is "+r.toString()+"\n");
                	
                    r.run();
                }
                catch (RuntimeException e) {
                    // You might want to log something here
                }
            }
        }
    }
    
    private class Mythread extends Thread{

		/* (non-Javadoc)
		 * @see java.lang.Thread#run()
		 */
		@Override
		public void run() {
			// TODO Auto-generated method stub
//			super.run();
			System.out.print("the"+getName()+"\n");
		}
    	
    }
    
    public static void  main(String[] aString){
    	TestPool testPool = new TestPool(100);
    	start();
    }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics