Home > Back-end >  Write a thread pool
Write a thread pool

Time:09-29

Demand background: because of the frequent to creating and destroying threads is the consumption of resources, the number of concurrent threads and difficult to control, so the team need to write a function of the thread pool, to manage the life cycle of a thread by oneself, and root according to busy degree of thread pool of threads in the pool for dynamic expansion,
Now project manager abstracts the thread pool object contains several major features (1) can be expansion and thread count, according to the number of tasks in your spare time only save coreSize number of threads in thread pool, and when coreSize threads have all at work, at this time if we add tasks to come in, will be a new thread, accept the task, until the number of threads in the pool is achieved when maxSize, not in the new thread (2) when the number of threads to maxSize, and all threads in a work state, if there are tasks added to come at this moment, it is stored in a Queue (can customize the Queue, can also be used on its own research and existing in the JDK Queue subclass) 3. If the number of tasks in the Queue have reached the maximum value (the Queue is full), have added to come over, at this time, discarding the strategy execution (can do nothing, also can still abnormal, it can also be called directly to the main thread run disposal, etc., it is best to use the strategy pattern rather than the if - else) 4. Define a thread of free time, if the thread pool finished processing tasks, thread idle, according to the defined new threads of free time, if time
Reached, recycling threads until the number of threads to achieve coreSize number for check, (pay attention to recycling operations such as atomic, prevent data inconsistency)

Ideas, allow to complete all functions in a class, then to face to the object code refactoring can directly read the JDK thread pool source code, reference to write a can (suggest) write a test class, test your thread pool is functioning normally

CodePudding user response:

His handwriting is too time consuming, the ExecutorService threadPool=Executors. NewCachedThreadPool () can understand, and you need some similar

CodePudding user response:

You can see the JDK source code, and then in one way or another

CodePudding user response:

Suggest you look at the current mainstream technology of the source code, this first, and then in this way, then it went,

CodePudding user response:

reference YangCheney reply: 3/f
suggest you look at the current mainstream technology of source code, this first, and then in this way, then it went,

I understand, I tried it on like that, then it knocked at, finally it was successful

CodePudding user response:

reference ^ @ lone Wolf @ ^ 4 floor response:
Quote: refer to the third floor YangCheney response:
suggest you look at the current mainstream technology of source code, this first, and then in this way, then it went,

I understand, I tried it on like that, then it knocked at, and finally it succeeded

This answer 666

CodePudding user response:

, thread pool tasks more than the original design of coreSize, put a queue, task queue full, have started new thread task execution, unless the number of threads has reached maxSize.
After they refused to strategy execution.

We who is wrong. Or your manager's design is like the original design to violate.

CodePudding user response:

refer to 6th floor softFE response:
, thread pool tasks more than the original design of coreSize, put a queue, task queue full, have started new thread task execution, unless the number of threads has reached maxSize.
After they refused to strategy execution.

We who is wrong. Or your manager's design is like the original design to violate.

Your understanding do not have what problem, the JDK Java thread pool. The util. Concurrent. The logic of ThreadPoolExecutor is indeed as you said core thread queue - - - the biggest thread - rejection policies. But their manager's design thoughts and tomcat thread pool is consistent, tomcat, the logic of the thread pool is core thread - the biggest thread - queue - rejection policies. Key source may have a look org.. Apache tomcat. Util. Threads. TaskQueue offers () method, shown in the following, don't discuss here Java. Util. Concurrent. The ThreadPoolExecutor the execute () method, anyone interested in to see.
 
@ Override
Public Boolean offer (Runnable o {
//we can 't do any checks
If (the parent==null) return. Super offer (o);
//we are maxed-out out on threads, simple queue the object
If (parent. GetPoolSize ()==parent. GetMaximumPoolSize ()) return. Super offer (o);
//we have idle threads, just add it to the queue
If (parent. GetSubmittedCount () & lt;=(parent. GetPoolSize ())) return. Super offer (o);
//if we have less threads than maximum force creation of a new thread
If (parent. GetPoolSize () & lt; The parent. GetMaximumPoolSize return false ());
//if we reached here, we need to add it to the queue
Return super. Offer (o);
}


  • Related