1 private static void createThreadPool() { 2 ExecutorService executorService = new ThreadPoolExecutor(2, 10, 3 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(5, true), 4 Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); 5 for (int i = 0; i < 10; i++) { 6 fin...
1privatestaticvoidcreateCachedThreadPool() {2ExecutorService executorService =Executors.newCachedThreadPool();3for(inti = 0; i < 10; i++) {4finalintindex =i;5executorService.execute(() ->{6//获取线程名称,默认格式:pool-1-thread-17System.out.println(DateUtil.now() + " " + Thread.currentT...
* ThreadFactory to create new threads when needed. * @param threadFactory the factory to use when creating new threads * @return the newly created thread pool * @throws NullPointerException if threadFactory is null */ public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { ret...
// 创建线程工厂ThreadFactory threadFactory=ThreadFactoryBuilder.create().setNamePrefix("myThread-").build();// 创建线程池ThreadPoolExecutor threadPoolExecutor=newThreadPoolExecutor(5,10,10,TimeUnit.SECONDS,newArrayBlockingQueue<>(100),threadFactory,newThreadPoolExecutor.AbortPolicy()); execute()方法 代...
threadPool = Executors.newSingleThreadExecutor();//单线程的线程池,只有一个线程在工作// threadPool = new ThreadPoolExecutor();//默认线程池,可控制参数比较多 private static void createCachedThreadPool() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i =...
1.newFixedThreadPool() 由于使用了LinkedBlockingQueue所以maximumPoolSize没用,当corePoolSize满了之后就加入到LinkedBlockingQueue队列中。 每当某个线程执行完成之后就从LinkedBlockingQueue队列中取一个。 所以这个是创建固定大小的线程池。 源码分析 public static ExecutorService newFixedThreadPool(int nThreads) { ...
This method returns false if the pool is stopped or * eligible to shut down. It also returns false if the thread * factory fails to create a thread when asked. If the thread * creation fails, either due to the thread factory returning * null, or due to an exception (typically OutOf...
newCachedThreadPool(r -> { Thread t = new Thread(r); t.setName(name + UUID.randomUUID().toString()); return t; }); } /** * 提供工厂方法 * * @param nThread * @param name */ public static BlockedThreadPool createBlockedThreadPool(int nThread, String name) { return new Blocked...
* create a thread is rejected */ Thread newThread(Runnable r); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Executors的实现使用了默认的线程工厂-DefaultThreadFactory。它的实现主要用于创建一个线程,线程的名字为pool-{poolNum}-thread-{threadNum} ...
Enter the total number ofwork queues that are serviced by this thread pool in the Number of Work Queues field. Click OK. Restart the Application Server. Equivalent asadmin command create-threadpool Previous: About Thread Pools Next: To edit a thread pool...