In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of thread pool ThreadPoolExecutor". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of thread pool ThreadPoolExecutor"?
1. Initialize the thread pool (4):
1. NewFixedThreadPool ()
Public final static ExecutorService esPool = Execustor.newFixedThreadPool (50)
Features: corePoolSize = = maxPoolSize, using LinkedBlockingQueue as the blocking queue; thread pool is not released when there are no executable tasks
2. NewCachedThreadPool ()
Public final static ExecutorService esPool = Execustor.newCachedThreadPool ()
Features: default cache 60s, the number of threads can reach Integer.MAX_VALUE, and SynchronousQueue is used internally as a blocking queue; when there are no executable tasks, reaching keepAliveTime will release thread resources, and new tasks will have to recreate new threads without executing idle threads, which will lead to system overhead; when in use, pay attention to control the number of concurrency and reduce the number of new threads created
3. NewScheduleThreadPool ()
Public final static ExecutorService esPool = Execustor.newScheduleThreadPool (50)
Features: periodic execution of submitted tasks within a specified period of time, which is generally used to synchronize data on a regular basis
4. NewSingleThreadExecutor ()
Features: initialize a thread pool with only one thread; internally use LinkedBlockingQueue as a blocking queue
2. Source code implementation:
1. ThreadPoolExecutor constructor
Public ThreadPoolExecutor (number of int corePoolSize,// core threads int maximumPoolSize,// maximum thread number long keepAliveTime,// thread survival time (when the number of threads must be between corePoolSize and maximumPoolSie TimeUnit unit,// time to take effect) unit of BlockingQueue workQueue,// time to live RejectedExecutionHandler handler// policy when refusing to process tasks) {this (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue) Executors.defaultThreadFactory (), handler) }
Description: blocking queue BlockingQueue:1) ArrayBlockingQueue (array structure bound blocking queue, FIFO); 2) LinkedBlockQueue (linked list structure blocking queue, FIFO); 3) SynchronousQueue (blocking queue without storing elements, each insertion must wait for another thread to remove); 4) PriorityBlockingQueue (blocking queue with priority).
Policies that refuse to handle the task RejectedExecutionHandler: 1), ThreadPoolExecutor.AbortPolicy (discard the current thread and throw a RejectedExecutionException exception) 2), ThreadPoolExecutor.DiscardPolicy (discard the current thread without throwing an exception), 3), ThreadPoolExecutor.DiscardOldestPolicy (discard the first task and then try the execution process again); 4), CallerRunnsPolicy (call the thread to perform this task)
2. Execute ()
Public void execute (Runnable command) {if (command = = null) throw new NullPointerException (); int c = ctl.get (); / / get the number of current threads: workerCountOf () if (workerCountOf (c) < corePoolSize) {if (addWorker (command, true)) / / addWorker return when the number of current threads is less than corePoolSize C = ctl.get ();} / / if the current thread is in Running state If (isRunning (c) & & workQueue.offer (command)) {int recheck = ctl.get (); / / check the thread pool (because thread resources may be released after the last check) and whether there are idle threads if (! IsRunning (recheck) & & remove (command) reject (command); else if (workerCountOf (recheck) = 0) addWorker (null, false);} / / if the current thread is in a non-Running state; else if (! addWorker (command, false) reject (command);}
3. Reject ()
Final void reject (Runnable command) {handler.rejectedExecution (command, this);}
4. Submit ()
Public Future submit (Callable task) {if (task = = null) throw new NullPointerException (); / / encapsulated into a FutureTask object, and the FutureTask class implements the Runnable interface RunnableFuture ftask = newTaskFor (task); / / executes execute (), which is submitted to the FutureTask thread pool through execute for execution, and finally executes FutureTask's run method execute (ftask); return ftask } protected RunnableFuture newTaskFor (Callable callable) {return new FutureTask (callable);}
5. Thread status
Private static final int COUNT_BITS = Integer.SIZE-3; / / that is, the thread pool in this state receives new tasks and handles tasks in blocking queues; private static final int RUNNING =-1
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.