Multi-threaded concurrent programming study notes 9 (XiaoDi Classroom) ------Thread pool and Executor framework

 

 

 

It will only use 10 threads. Because we set its capacity.

 

We now set this queue capacity to 20.

 

We can see here that it uses 20 threads. But something went wrong, we will learn about this later. 

Let's use our callable now:

 

 

Generally, if we want to get a return value after the thread is executed, we can use future.

 

We found that the thread pool does not output the tasks of our queue.

We now add the thread pool method:

 

Using this method, we can display the tasks in our queue.

 

 

 

<=cSize threads will be started and other tasks will be placed in the work queue.

 What if x-nworks>mSize?

mSize threads will be executed to perform the task, and the rest will execute the corresponding rejection policy.

Of course, we can also customize the thread pool rejection policy:

We can add our custom rejection policy to the previous demo.

Will output the content in our custom exception.

In actual work, we can do our log printing or email warnings here.

 Executor framework:

 

 So what are the differences between these different ways of creating a thread pool?

 Suggestions for using the thread pool:

 

Create such a class.

Then we go to set the maximum heap memory for it:

 

 Then we go to run it:

An exception will be reported.

 

We will find this file in the path we set earlier.

 

Here we can use our tool to analyze this oom exception:

 We click Accept.

 

 

Find the previous file.

 

We can see our occupancy situation, but we will not introduce the use of this tool in detail here.

Such a code will also cause memory overflow.

 

 

For such a piece of code, there is an exception in our code.

 

Using submit did not give us any abnormal output.

We now use execute to run:

 

 

An exception will be reported to us.

 

 

In this case, submit will also cause us an exception.

 

 

Guess you like

Origin blog.csdn.net/weixin_52618349/article/details/130211410
Recommended