A very simple, very short thread pool

xiaoxiao2021-03-06  68

Recently I have written a HTTP proxy server, I found a lot of connections when accessing the webpage, and the consumption thread is very much, for the system

A small overhead. And these threads have a short time, more than 99% threads exist in milliseconds, relatively

The deregistration of threads accounts for the vast majority of CPU time. Therefore, in the Internet, the information of the thread pool is found. It is not too big to be too complicated. It is not a professional.

Level. No way, I have to learn the wait () / notify () mechanism, write a small thread pool. The code is as follows (a total of 2 classes): / * * A simple thread pool threadpool .java * / public class threadpool {// The following is the configuration information, you can change static int max_thread = 1000; // Not used Static int min_thread = 14; static int id = 1; // thread ID number, mainly used to monitor threads where static private ThreadPool pool = new ThreadPool (); static public ThreadPool getThreadPool () {return pool;} Stack stack = new Stack (MIN_THREAD); private ThreadPool () {} synchronized public boolean putWorkThread (WorkThread wt ) {if (stack.size ()

}}}} How to use: runnable r = ...; new thread (r) .Start (); this is your previous code, using the thread pool only to turn the latterPool.getthreadPool (). getWorkthread (). Start (r); it is OK, the other code does not have any changes.

There are still two functions that have no implementation: 1. Control of the maximum thread.

Because learning Java does not understand, there may be a place that is not very perfect, please also ask your high-end guidance, thank you !!

转载请注明原文地址:https://www.9cbs.com/read-90388.html

New Post(0)