Java并发 专题
您的位置:java > Java并发专题 > Java并发newFixedThreadPool方法
Java并发newFixedThreadPool方法
作者:--    发布时间:2019-11-20

可以通过调用executors类的static newfixedthreadpool()方法获得一个固定线程池。

语法

executorservice fixedpool = executors.newfixedthreadpool(2);

其中,

  • 最多2个线程将处于活动状态。
  • 如果提交了两个以上的线程,那么它们将保持在队列中,直到线程可用。
  • 如果一个线程由于执行关闭期间的失败而终止,则执行器尚未被调用,则创建一个新线程。
  • 线程会一直存在,直到池关闭。

示例

以下testthread程序显示在线程环境中使用newfixedthreadpool方法。

import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
import java.util.concurrent.threadpoolexecutor;
import java.util.concurrent.timeunit;

public class testthread {

   public static void main(final string[] arguments) throws interruptedexception {
      executorservice executor = executors.newfixedthreadpool(2);

      // cast the object to its class type
      threadpoolexecutor pool = (threadpoolexecutor) executor;

      //stats before tasks execution
      system.out.println("core threads: " + pool.getcorepoolsize());
      system.out.println("largest executions: "
         + pool.getlargestpoolsize());
      system.out.println("maximum allowed threads: "
         + pool.getmaximumpoolsize());
      system.out.println("current threads in pool: "
         + pool.getpoolsize());
      system.out.println("currently executing threads: "
         + pool.getactivecount());
      system.out.println("total number of threads(ever scheduled): "
         + pool.gettaskcount());

      executor.submit(new task());
      executor.submit(new task());

      //stats after tasks execution
      system.out.println("core threads: " + pool.getcorepoolsize());
      system.out.println("largest executions: "
         + pool.getlargestpoolsize());
      system.out.println("maximum allowed threads: "
         + pool.getmaximumpoolsize());
      system.out.println("current threads in pool: "
         + pool.getpoolsize());
      system.out.println("currently executing threads: "
         + pool.getactivecount());
      system.out.println("total number of threads(ever scheduled): "
         + pool.gettaskcount());

      executor.shutdown();
   }  

   static class task implements runnable {

      public void run() {
         try {
            long duration = (long) (math.random() * 5);
            system.out.println("running task! thread name: " + thread.currentthread().getname());
               timeunit.seconds.sleep(duration);
            system.out.println("task completed! thread name: "+ thread.currentthread().getname());
         } 
         catch (interruptedexception e) {
            e.printstacktrace();
         }
      }
   }
}

这将产生以下结果 -

core threads: 2
largest executions: 0
maximum allowed threads: 2
current threads in pool: 0
currently executing threads: 0
total number of threads(ever scheduled): 0
core threads: 2
largest executions: 2
maximum allowed threads: 2
current threads in pool: 2
currently executing threads: 1
total number of threads(ever scheduled): 1
running task! thread name: pool-1-thread-1
running task! thread name: pool-1-thread-2
task completed! thread name: pool-1-thread-1
task completed! thread name: pool-1-thread-2

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册