线程池实例:继承threadgroup -凯发k8国际

`
zw7534313
  • 浏览: 1232863 次
  • 性别:
  • 来自: 北京
博主相关
  • 博客
  • 微博
  • 相册
  • 收藏
  • 社区版块
    • ( 0)
    • ( 21)
    • ( 1)
    存档分类
    最新评论

    线程池实例:继承threadgroup

      博客分类:
    • java

     

    线程池的作用:

         线程池作用就是限制系统中执行线程的数量。
         根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了;否则进入等待队列。

     

    为什么要用线程池:

    1. 减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务
    2. 可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1mb内存,线程开的越多,消耗的内存也就越大,最后死机)

    import java.util.linkedlist;


    public class threadpool extends threadgroup{
     private boolean isclosed = false;           //线程池是否关闭
     private linkedlist workqueue;     //工作队列
     private static int threadpoolid = 1;        //线程池的id

     public threadpool(int poolsize){            //poolsize 表示线程池中的工作线程的数量
      super(threadpoolid "");                  //指定threadgroup的名称
      setdaemon(true);                //继承到的方法,设置是否守护线程池
      workqueue = new linkedlist();     //创建工作队列
      for(int i=0;i< poolsize; i ){
       new workthread(i).start();     //创建并启动工作线程,线程池数量是多少就创建多少个工作线程
      }
     }

     public synchronized void execute(runnable task){
      if(isclosed){
       throw new illegalstateexception();
      }
      if(task != null){
       workqueue.add(task); //向队列中加入一个任务
       notify();            //唤醒一个正在gettask()方法中待任务的工作线程
      }
     }

     private synchronized runnable gettask(int threadid) throws interruptedexception{
      while(workqueue.size()==0){
       if(isclosed)
        return null;
       system.out.println("工作线程" threadid "等待任务...");
       wait();
      }
      system.out.println("工作线程" threadid "开始执行任务...");
      return (runnable) workqueue.removefirst();     //返回队列中第一个元素,并从队列中删除
     }

     public synchronized void closepool(){
      if(! isclosed){ 
       waitfinish();     //等待工作线程执行完毕
       isclosed = true;
       workqueue.clear();  //清空工作队列
       interrupt();         //中断线程池中的所有工作线程,此方法继承自threadgroup类
      }
     }

     public void waitfinish(){
      synchronized(this){
       isclosed =true;
       notifyall();         //唤醒所有还在gettask()方法中等待任务的工作线程
      }
      thread[] threads = new thread[activecount()]; //activecount() 返回该线程组中活动线程的估计值。
      int count = enumerate(threads);      //enumerate()方法继承自threadgroup类,根据活动线程的估计值获得线程组中当前所有活动的工作线程
      for(int i=0; i< count; i ){
       try{
        threads[i].join(); //等待工作线程结束
       } catch(interruptedexception ex){
        ex.printstacktrace();
       }
      }
     }

     private class workthread extends thread{
      private int id;
      public workthread(int id){
       //父类构造方法,将线程加入到当前的threadpool线程组中
       super(threadpool.this, id "");
       this.id=id;
      }
     
      public void run(){
       while(! isinterrupted()){  //isinterrupted() 方法继承自thread类,判断线程是否被中断
        runnable task = null;
        try{
         task = gettask(id);  //取出任务
        }catch(interruptedexception ex){
         ex.printstacktrace();
        }
        //如果gettask()返回null或者线程执行gettask()时被中断,则结束此线程
        if(task == null)
         return;
        try{
         task.run();  //运行任务
        } catch(throwable t){
         t.printstacktrace();
        }
       }
      }
     }

     

     public static void main(string[] args) throws interruptedexception{
      threadpool threadpool = new threadpool(3); //创建一个有3个工作线程的线程池
      thread.sleep(500);  //休眠500毫秒,以便让线程池中的工作线程全部运行
      //运行任务
      for(int i=0; i<=5; i ){ //创建6个任务
       threadpool.execute(createtask(i));
      }
      system.out.println("......................................");
      threadpool.waitfinish();  //等待所有任务执行完毕
      threadpool.closepool();   //关闭线程池

      system.out.println("game over!");
     }
     
     private static runnable createtask(final int taskid){
      return new runnable(){
       public void run(){
        try {
         thread.sleep(20);
        } catch (interruptedexception e) {
         e.printstacktrace();
        }
        system.out.println(taskid ": hello");
       }
      };
     }
    }

    分享到:
    |
    评论

    相关推荐

      null 博文链接:https://bijian1013.iteye.com/blog/2284676

      java线程池实例java线程池实例e:\users\administrator\workspace

      执行一个异步任务你还只是如下new thread吗?是不是太low 了一点? 我这里有四种线程池的案例轻松让你理解和使用线程池。

      线程池例子线程池例子线程池例子线程池例子

      比较完整的线程池实例代码,linux系统下完美运行

      所以源代码都有 ,一个完整的线程池的实例

      1.线程池管理器(threadpoolmanager):用于创建并管理线程池 2.工作线程(workthread): 线程池中线程 3.任务接口(task):每个任务必须实现的接口,以供工作线程调度任务的执行。 4.任务队列:用于存放没有处理的...

      线程池详细讲解的很好实例!

      简单实用的java线程池实例代码。包括测试代码和工程文件。

      一个c 线程池类的使用。包含一个线程池类,工程使用vc6.0编译器,整个程序演示了怎么使用一个线程池。

      delphi线程池mcpage实例

      [1]中博主自己通过threadgroup实现一个线程池(挺方便理解的),使用的是jdk1.4版本,jdk1.5版本以上提供了现成的线程池。 [2]中介绍了java.util.concurrent.executors类的api。 [3]中介绍了java中线程池的类体系...

      在c#中可以通过system.threading.threadpool类来实现,在默认情况下,threadpool最大可建立500个工作线程和1000个i/o线程(根据机器cpu个数和.net framework版本的不同,这些数据可能会有变化) ...

      vc 多线程实例、线程池原理及实例相关资料汇总大全

      java100例之实例60 继承thread实现多线程

      delphi线程池的使用实例,带有详细的日志记录,方便初学者学习。

      windows下一个比较完美的线程池实现和示例 本线程池提供了如下功能: 1.能根据任务个数和当前线程的多少在最小/最大线程个数之间自动调整(vista后的系统有 setthreadpoolthreadmaximum 等函数有类似功能); 2.能方便...

      自已用qt写的线程池实现程序,可以正常运行,用socket进行通讯,很适合初学者学习借鉴;

      activemq与spring线程池整合的一个实例。 lib库没有上传。 对于实例的讲解,在竹子的论坛有我对这个实例的帖子(http://www.java2000.net/viewthread.jsp?tid=1167) lib中包含: apache-activemq-4.1.1.jar ...

      一个简单的线程池例子;

    global site tag (gtag.js) - google analytics
    网站地图