The difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();

后端 未结 6 653
醉酒成梦
醉酒成梦 2020-12-23 16:57

Well title says it, what is the difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();

6条回答
  •  爱一瞬间的悲伤
    2020-12-23 17:16

    I prefer to use ExecutorService or ThreadPoolExecutor even for single digit threads. They offer more flexibility.

    Have a look at ExecutorService & ThreadPoolExecutor sections in related SE questions :

    java Fork/Join pool, ExecutorService and CountDownLatch

    Java's Fork/Join vs ExecutorService - when to use which?

    Assume that you have started with your own thread instead of ExecutorService. In future, if there is a need for supporting multiple threads, ExecutorService or ThreadPoolExecutor will offer better control and flexibility for you. You can fine-tune required number of parameters in these below APIs.

    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
    TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory,
    RejectedExecutionHandler handler)
    

提交回复
热议问题