Java thread pool implementation

Source: Internet
Author: User
Package COM. lxw. thread; import Java. util. writable list; public class threadpool {private writable list <mythread> List = new writable list <mythread> (); // default thread pool quantity: Private Static final int deflautpoolsize = 10; public threadpool (INT poolsize) {for (INT I = 0; I <poolsize; I ++) {list. add (New mythread () ;}} public threadpool () {This (deflautpoolsize);} public synchronized void destoryallthread () {for (INT I = 0; I <li St. size (); I ++) {list. get (I ). setstop (true) ;}notifyall () ;}// obtain an available thread in the thread pool. You pass an object that implements the runnable interface and call the start method, will call this object's run method public thread getthread (runnable R) {If (list. size ()! = 0) {mythread thread = List. removefirst (); thread. setrunnable (r); Return thread;} Throw new runtimeexception ("no thread in the thread pool is available");}/** a special thread, it is a thread that can be reused in the thread pool * generally, there are three methods to enhance a class: 1, become its subclass 2, decoration mode (IO stream in JDK is) 3, proxy mode * the method used in this example is the first */class mythread extends thread {private runnable; private Boolean stop = true; private Boolean isstart = false; public void setstop (Boolean stop) {stop = stop; Start ();} private voi D setrunnable (runnable R) {runnable = r ;}@ overridepublic synchronized void start () {If (! Isstart) {isstart = true; stop = false; Super. Start () ;}else {Policy () ;}@ overridepublic synchronized void run () {While (! Stop) {runnable. run (); try {list. addlast (this); this. wait ();} catch (interruptedexception e) {Throw new runtimeexception (e) ;}}// test the mian method public static void main (string [] ARGs) throws interruptedexception {threadpool = new threadpool (3); thread T1 = threadpool. getthread (New testthread (5); t1.start (); threadpool. getthread (New testthread (15 )). start (); threadpool. getthread (New testthread (25 )). start (); thread. sleep (5000); threadpool. getthread (New testthread (35 )). start (); threadpool. getthread (New testthread (45 )). start (); thread. sleep (3000); threadpool. getthread (New testthread (35 )). start (); threadpool. getthread (New testthread (45 )). start (); threadpool. getthread (New testthread (35 )). start (); thread. sleep (3000); threadpool. destoryallthread ();}/** a common thread, used for testing */static class testthread implements runnable {private int count; Public testthread (INT count) {This. count = count;} public testthread () {super (); // todo auto-generated constructor stub} @ overridepublic void run () {system. out. println (count ++ ""); system. out. println (count ++ ""); system. out. println (count ++ ""); system. out. println (count ++ ""); system. out. println ("threadname:" + thread. currentthread (). getname ());}}}

The implementation of this thread pool is entirely based on the idea of the JDBC connection pool. In fact, I don't know how others actually implement the thread pool, but I am feasible... This thread pool is completely writable and only a fixed thread is available for learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.