为什么要用Executors.defaultThreadFactory().newThread(run);创建线程?

Executors

/**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
}

private ExecutorService proThreadPool = Executors.newFixedThreadPool(50);

时间: 2024-08-27 18:31:15

为什么要用Executors.defaultThreadFactory().newThread(run);创建线程?的相关文章

高并发之——从源码角度分析创建线程池究竟有哪些方式

前言 在Java的高并发领域,线程池一直是一个绕不开的话题.有些童鞋一直在使用线程池,但是,对于如何创建线程池仅仅停留在使用Executors工具类的方式,那么,创建线程池究竟存在哪几种方式呢?就让我们一起从创建线程池的源码来深入分析究竟有哪些方式可以创建线程池. 使用Executors工具类创建线程池 在创建线程池时,初学者用的最多的就是Executors 这个工具类,而使用这个工具类创建线程池时非常简单的,不需要关注太多的线程池细节,只需要传入必要的参数即可.Executors 工具类提供了

通过线程池创建线程

package thread; import java.util.concurrent.*; /** * @auto dh * @create 2020-03-29-0:04 */class Th0009 implements Callable<Integer>{ private int sum=0; public Integer call(){ for(int i=1;i<=100;i++){ sum+=i; } return sum; }}class Th009 implements

【搞定面试官】你还在用Executors来创建线程池?会有什么问题呢?

前言 上文我们介绍了JDK中的线程池框架Executor.我们知道,只要需要创建线程的情况下,即使是在单线程模式下,我们也要尽量使用Executor.即: ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1); //此处不该利用Executors工具类来初始化线程池 但是,在<阿里巴巴Java开发手册>中有一条 [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方

java多线程(三)-Executors实现的几种线程池以及Callable

从java5开始,类库中引入了很多新的管理调度线程的API,最常用的就是Executor(执行器)框架.Executor帮助程序员管理Thread对象,简化了并发编程,它其实就是在 提供了一个中间层,方便程序员管理异步任务的执行,而又不用显式的管理线程的生命周期. Executor采用了线程池实现,也更节约开销,因为是我们启动新线程的首选方法. 示例代码:src/thread_runnable/CachedThreadPool.java 1 public class CachedThreadPo

Java多线程之~~~使用ThreadPoolExecutor来创建线程

以前我们创建线程的时候都是主动的new一个Thread,然后调用他们的start方法,但是如果线程非常多,任务也非 常多的时候,这样写就会显得非常麻烦,当然可能效率也不是很高,Java给我们提供了叫线程创建器这个样概念的类, 他可以帮助我们管理这些线程,你做的就是编写好代码,然后交给他,她就会自动帮你运行. 当然,带cache的threadpool 对于死掉的线程重新调用,在性能上也会有非常好的表现,但是不能将太多的线程交 给他管理,否则就会把系统拖垮,下面我们来做一个例子. package c

使用java.util.concurrent.ThreadFactory类创建线程

工厂设计模式是Java中最常用的设计模式之一.它是一种创建型设计模式,能够用于创建一个或多个类所需要的对象.有了这个工厂,我们就能集中的创建对象. 集中创建方式给我们带来了一些好处,例如: 1. 能够很容易的改变类创建的对象或者创建对象的方式: 2. 能够很容易限制对象的创建,例如:我们只能为a类创建N个对象: 3. 能够很容易的生成有关对象创建的统计数据. 在Java中,我们通常使用两种方式来创建线程:继承Thread类和实现Runnable接口.Java还提供了一个接口,既ThreadFac

java 创建线程

java创建线程有3种方式: (1)继承Thread(2)实现Runnable接口(3)实现Callable接口 1.继承Thead package com.java.thread; public class ThreadClient { public static void main(String[] args) { Print p1 = new Print(); Print p2 = new Print(); p1.start(); p2.start(); } } class Print e

Java创建线程的两种方法

大多数情况,通过实例化一个Thread对象来创建一个线程.Java定义了两种方式: 实现Runnable 接口: 可以继承Thread类. 下面的两小节依次介绍了每一种方式. 实现Runnable接口 创建线程的最简单的方法就是创建一个实现Runnable 接口的类.Runnable抽象了一个执行代码单元.你可以通过实现Runnable接口的方法创建每一个对象的线程.为实现Runnable 接口,一个类仅需实现一个run()的简单方法,该方法声明如下:    public void run( )

创建线程的第三种方式——使用Callable接口

Callable是类似于Runnable的接口,实现Callable的类和实现Runnable的类都是可被其他线程执行的任务. 优点:有返回值 缺点:实现繁琐 简单实现: CallableAndFuture.java /** * 简单实现 * * @author :liuqi * @date :2018-06-13 11:10. */ public class CallableAndFuture { static class MyThread implements Callable<String