Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java thread pool

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use the Java thread pool". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Java thread pool.

Invisible coupling between tasks and execution strategies

Dependent task

Tasks that use the thread closure mechanism

Tasks that are sensitive to the corresponding time

Tasks that use ThreadLocal

In a thread pool, deadlocks may occur if the task depends on other tasks.

Listing 8-1 deadlock occurs on tasks in a single thread

Public class ThreadDeadLock {Executor executor = Executors.newSingleThreadExecutor (); public class RenderPageTask implements Callable {@ Override public String call () throws Exception {Future header, footer; header = executor.execute (new LoadFileTask ("header.html")); footer = executor.execute (new LoadFileTask ("footer.html")); String page = renderBody () / / deadlock will occur-because the task is waiting for the result of the subtask return header.get () + page + footer.get ();}

Whenever you submit a dependent Executor task, be aware that thread "hunger" deadlocks may occur, so you need to record thread pool size or configuration limits in your code or in the configuration file that configures Executor.

In addition to the display restrictions on the thread pool size, there may be some implicit restrictions due to constraints on other resources. If your application uses a JDBC connection pool of 10 connections and each task requires a database connection, the thread pool seems to have only 10 threads, because when there are more than 10 tasks, the new task needs to wait for other tasks to release the connection.

Tasks that take longer to run

Set the size of the thread pool

Configure ThreadPoolExecutor

ThreadPoolExecutor is the real implementation of thread pool, and its constructor provides a series of parameters to configure thread pool.

Public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) {.} public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHander hander) {.}

The number of core threads in the corePoolSize-- thread pool, and by default, core threads survive in the thread pool all the time, even if they are in a restricted state. If you set the allowCoreThreadTimeOut property of ThreadPoolExecutor

Set to true, then the idle thread core thread will have a timeout policy while waiting for a new task to arrive. This interval is specified by keepAliveTime. When the wait time exceeds the time specified by keepAliveTime, the core thread will be terminated.

The maximum number of threads that the maximumPoolSize-- thread pool can hold, and when the number of active threads reaches this value, subsequent new tasks will be blocked.

The timeout for keepAliveTime-- non-core threads to be idle, beyond which the non-core threads will be reclaimed. When the allowCoreThreadTimeOutvalue of the ThreadPoolExecutor property is true, keepAliveTime also acts on the core current process.

Unit-- is used to specify the units of the keepAliveTime time parameter, which is an enumeration, such as TimeOut.MILLISECONDS (milliseconds), TimeOut.SECONDS (seconds), TimeOut.MINUTES (minutes), and so on.

The task queue in the workQueue-- thread pool, and Runnable objects submitted through the thread pool's executor method are stored in this parameter.

ThreadFactory-- thread factory, which provides the ability to create new threads for thread pools. ThreadFactory is an interface that has only one method: Thread newThread (Runnable r)

If hander-- is not often used, no introduction will be made.

ThreadPoolExecutor roughly follows the following rules when performing tasks:

1 if the number of threads in the thread pool does not reach the core number, then a core thread is directly started to execute the task.

2 if the number of threads in the thread pool has reached or exceeded the number of cores, the task will be inserted into the task queue for execution.

3 if the task cannot be inserted into the task queue in step 2, it is often because the task queue is full, and if the number of threads does not reach the maximum specified by the thread, a non-core thread will be started to execute the task immediately.

4 if the number of threads in step 3 has reached the maximum number of threads specified by the thread pool, then the task is refused, and ThreadPoolExecutor calls the rejectedExecution method of RejectedExecutionHander to notify the caller.

At this point, I believe that you have a deeper understanding of the use of Java thread pool. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report