java简单线程池实例代码


 

package aa;

 

import java.util.Random;

 

public class DownThread extends Thread {

 

    private boolean runFlag;

 

    public boolean isRunFlag() {

       return runFlag;

    }

    public DownThread(){

       System.out.println(this.getName()+" 初始化");

       this.setRunFlag(false);

    }

 

    public synchronized void setRunFlag(boolean runFlag) {

       this.runFlag=runFlag;

       if(runFlag)this.notify();

    }

 

    public synchronized void run(){

       while (true){

           if(!this.isRunFlag()){

              try{

                  this.wait();

              }catch(InterruptedException e){

                  e.printStackTrace();

              }

           }

           System.out.println(this.getName()+"doingsomething!");

           Random ran=new Random();

           try{

              long l=10001*(ran.nextInt(10)+1);

              System.out.println("这活要干 "+l+"毫秒");

              sleep(l);

           }catch(InterruptedException e){

              e.printStackTrace();

           }

           System.out.println(this.getName()+"done!");

           this.setRunFlag(false);

           System.out.println(this.getName()+"回收");

           ThreadPool.removeThread(this);

       }

    }

}


============================================================================

 

package aa;

 

import java.util.LinkedList;

 

public class ThreadPool {

 

    final static int MaxLength=20;

   

    private static LinkedList pool=null;

   

    public ThreadPool(){

       System.out.println("初始化线程池="+MaxLength);

       pool=new LinkedList();

       for(int i=0;i<MaxLength;i++){

             DownThread thread=new DownThread();  

             removeThread(thread);

             thread.start();

       }

    }

 

    public boolean isFull(){

       return pool.isEmpty();

    }

   

    public void addThread(){

       synchronized (pool) {

           while(this.isFull()){

              System.out.println("线程池满,等待中。。。。。");

              try{

                  pool.wait();

              }catch(InterruptedException e){

                  e.printStackTrace();

              }

           }

           DownThread thread=(DownThread)pool.removeFirst();

           if(!thread.isRunFlag()){

              System.out.println(thread.getName()+"is processing");

              thread.setRunFlag(true);

           }

       }

    }

   

    public static void removeThread(DownThread downThread) {

        synchronized (pool) {

           pool.addLast(downThread);

           pool.notify();

       }

    }

   

    public static void main(String[] args){

       ThreadPool test=new ThreadPool();

       for(int i=0;i<1000;i++){

           test.addThread();

       }

    }

}

package aa;

 

public class TestThread  extends Thread{

 

    public static void main(String args[]){

       Runner r=new Runner();

       Thread t1=new Thread(r);

       Thread t2=new Thread(r);

       t1.start();

       t2.start();

    }

}

    class Runner implements Runnable{

       public Runner(){

           super();

       }

       public void run(){

           for(int i=0;i<20;i++){

              System.out.println("NO."+i);

           }

       }

    }

 

 

posted @ 2013-06-13 01:00  廖世勇  阅读(263)  评论(0编辑  收藏  举报