Copyright: hard code word, when you reprint remember to tell me https://blog.csdn.net/dxyinme/article/details/90743223
When to start Java multi-threaded, personal feeling directly start a thread for thread management is very negative, and then went to check out if there is a more convenient way to manage threads.
There really seems to search a bit, mainly these two libraries:
there are the following four kinds of thread pool types
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
You can create a thread pool by:
ExecutorService xxx = Executors.newCachedThreadPool();
You can also
xxx.isTerminated()
Thread determines whether the thread pool are terminated.
able to pass
xxx.shutdown()
Send a signal to terminate the process thread pool xxx, xxx later when all existing threads will not be affected, but can not go down after xxx execute in the new thread; after xxx threads all over, xxx.isTerminated () will be set to True.
xxx.execute(new Runnable())
Xxx will put a thread pool thread execution.