zoukankan      html  css  js  c++  java
  • 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);

               }

           }

        }

     

  • 相关阅读:
    Redis 介绍1
    浅议javascript的内存泄露
    Redis 介绍2——常见基本类型
    常见的排序算法二——希尔排序
    Mono 学习之旅二
    Mono 学习之旅一
    常见的排序算法三——冒泡排序
    微软的面试题
    八大排序算法总结 1直接插入排序
    windows phone7 项目一俄罗斯方块源码 及说明
  • 原文地址:https://www.cnblogs.com/liaoshiyong/p/3150869.html
Copyright © 2011-2022 走看看