Java secure coding guide: the use of ThreadPool

Introduction @ h_ 502_ 21 @ in Java, in addition to using a single thread, we also use ThreadPool to build a thread pool. What should we pay attention to in the process of using thread pool? Let's have a look.

Java's own thread pool @ h_ 502_ 21 @ Java provides a very useful tool class executors. Through executors, we can easily create a series of thread pools: executors Newcachedthreadpool. You can create a thread pool for new threads as needed. Threads created in the thread pool may be used to complete another task after completing one task. Executors. Newfixedthreadpool (int nthreads) to create a thread pool with a reusable fixed number of threads. This thread pool contains a maximum of nthread threads. Executors. Newsinglethreadexecutor(), create an executor that uses a single worker thread. Even if there are many tasks, only one thread can complete the task. Executors. Newsinglethreadscheduledexecutor(), creates a single threaded executor that can schedule commands to run after a given delay or execute periodically.

If the thread submitted to the thread pool can be interrupted, @ h_ 502_ 21 @ executorservice thread pool provides two convenient methods to stop threads in the thread pool: shutdown and shutdown now. Shutdown will not accept new tasks, but will wait for existing tasks to complete. Shutdown now attempts to terminate an existing running thread immediately. So how does it work? Let's look at an implementation of a ThreadPoolExecutor: public list < runnable > shutdown now(){

Correctly handle the exception @ h of threads in the thread pool_ 502_ 21 @ if an exception occurs to the thread in the thread pool, such as runtimeException, how can we catch it? If you can't handle the exception properly, you will have unpredictable problems. Take the following example: public void wrongsubmit() throws interruptedexception{

When using ThreadLocal in the thread pool, be sure to clean up @ H_ 502_ 21 @ we know that ThreadLocal is a local variable in the thread. If we use ThreadLocal during the running of the thread, the previously set variables will be read when the thread is recycled and performs other tasks again, resulting in unknown problems. The correct way to use it is to call the remove operation of ThreadLocal after each task is executed by the thread. Or in the custom ThreadPoolExecutor, override the beforeexecute (thread T, runnable R) method and add the remove operation of ThreadLocal. The code of this article: learn-java-base-9-to-20 / tree / Master / security, which has been included in this article http://www.flydean.com/java-security-code-line-threadpool/ The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to find! Welcome to my official account: "those things in procedure", understand technology, know you better!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>