With the increasing number of CPU cores, it is unavoidable to use multithreading technology to make full use of its computing power. Therefore, multithreading technology is the technology that the server developer must master. The creation and destruction of threads involves system calls and consumes system resources, so threading pool technology is introduced to avoid frequent thread creation and destruction. In Java, with a executors tool class, you can create a thread pool for us, which is essentially new with a Threadpoolexecutor object. The thread pool is almost certainly a question of interview. This section combines the source code to say how Threadexecutor works
One, thread pool creationLet's take a look at the most complete construction method of the Threadpoolexecutor parameter: ①corepoolsize: The thread pool's core threads, in other words, even if the line constructor no task, there will be corepoolsize threads waiting for the task. ②maximumpoolsize: Maximum number of threads, no matter how many tasks you submit, the number of threads constructor up to the working thread is maximumpoolsize. ③keepalivetime: The duration of the thread's survival. When the number of threads constructor is greater than corepoolsize, the thread exits if no task is available to wait for the KeepAliveTime to execute. ⑤unit: This is used to specify KeepAliveTime units, such as seconds: Timeunit.seconds. ⑥workqueue: A blocking queue in which the submitted tasks will be placed in this queue. ⑦threadfactory: Thread factory, used to create threads, primarily to give the thread name, the default factory thread name: pool-1-thread-3. ⑧handler: The Deny policy is called when the thread constructor threads are exhausted and the queue is full. These are the parameters that are used to create the thread pool, and often the interviewer asks the question.
second, thread pool execution processHere in a diagram, the thread pool execution process task is submitted to the thread pool, will first determine whether the current number of threads is less than corepoolsize, if less than the creation of a thread to perform the submitted task, otherwise put the task into the Workqueue queue, if the Workqueue full, Determines whether the current number of threads is less than maximumpoolsize, and if it is less than the thread execution task, the handler is called to indicate that the thread pool refuses to receive the task. Here take jdk1.8.0_111 source code for example, look at the specific implementation.
1, first look at the thread pool executor method①: Determines whether the current number of active threads is less than corepoolsize, and if it is less than, call Addworker to create a thread to perform the task ②: if it is not less than corepoolsize, add the task to the Workqueue queue. ③: If the workqueue fails, the thread execution task is created, and if the creation thread fails at this point (the current number of threads is not less than maximumpoolsize), reject (internal call handler) is called to reject the task.
2, and then look at the Addworker method to achieveThis code is when creating a non-core thread, that is, the core equals false. Determines whether the current number of threads is greater than or equal to Maximumpoolsize, and returns false if greater than equals, that is, the creation of a thread in the ③ that is mentioned above. The lower half of the Addworker method: ① Creates a Worker object, and also instantiates a thread object. ② boot up this thread
3, then to the worker to see its implementationYou can see that when you create a worker, you call threadfactory to create a thread. Starting a thread in the upper ② triggers the worker's Run method to be called by the thread.
4. Let's take a look at the logic of the Runworker methodThe thread calls Runwoker, and the while loop calls the Gettask method to read the task from the Workerqueue and then executes the task. This thread does not exit as long as the Gettask method does not return null.
5, finally look at the Gettask method realization① we don't care about Allowcorethreadtimeout, the default value for this variable is false. Wc>corepoolsize is to determine whether the current number of threads is greater than corepoolsize. ② if the current number of threads is greater than corepoolsize, the poll method of Workqueue is called to get the task, and the time-out is keepalivetime. If the keepalivetime time is exceeded, poll returns NULL, and the above mentioned while sequence exits, and the thread executes. If the current number of threads is less than corepoolsize, then Workqueue's Take method is called to block at the current.
The working principle of thread pool and the interpretation of source code