Since we have recently started to learn Java, we have used the thread pool to write out this Java version of the thread pool according to the previous C + + notation.
Taskrunnale implements the interface of the related task, and realizes what task is implemented in the corresponding run function.
Package Threadpool;import Java.util.arraydeque;import Java.util.arraylist;import java.util.list;import Java.util.queue;class Taskrunnale implements Runnable {public void run () {System.out.println (This.hashcode () + "Testrunner in Works"); This method causes the thread to sleep 5 seconds synchronized (this) {try {wait (1000);//wait 5 seconds } catch (Interruptedexception IoE) {}} System.out.println (THIS.HASHC Ode () + "Testrunner out Works"); }}class Thread_inpool extends thread{private ThreadPool pool_ = null; The pool thread needs to know its own pool private Runnable target_ = null; The task of the thread private Boolean shutdown_ = false; Private Boolean Idle_ = false;//Set whether the thread is waiting for public thread_inpool (ThreadPool spool) {super (); Pool_ = spool; Start (); The public void Run () {//This loop cannot end unless the pool class requires the thread to end//each cycle will perform a pool class assigned to the task target while (!shutdown _) {Idle_ = false; if (target_! = null) {Target_.run (); Run the code in target target_ = null; if (Pool_. Switchthread (This) = = 1) continue; } else {idle_ = true; Synchronized (this) {try {wait (2000); /wait 2 second time} catch (Interruptedexception IoE) { }}}}//The loop cannot end here, otherwise the thread ends, the resource is withdrawn by the VM,//It does not function as a thread pool} Public synchronized void Settarget (java.lang.Runnable newtarget) {//Set new target and wake thread in sleep target_ = NewT Arget; New Task Notifyall (); Wake-Up Sleep thread} public synchronized void ShutDown () {shutdown_ = true; Notifyall (); }}public class ThreadPool {private static ThreadPool Instance_ = null; public static final int low_priority = 0; public static final int normal_priority = 1; public static final int high_priority= 2; private static int m_threadcount = 4; Private list<thread_inpool> M_idlethread = new arraylist<thread_inpool> (); Idle thread private list<thread_inpool> m_activethread = new arraylist<thread_inpool> ();//thread in task execution private queue<runnable> M_runabletarger = new arraydeque<runnable> (); ;//Task queue, here with queue, is given the ability to have a task priority extension private ThreadPool (int initcount) {m_threadcount = initcount > 0?initcount:4 ; for (int i = 0;i< m_threadcount; i++) {M_idlethread.add (new Thread_inpool (this)); }} public static ThreadPool get_instance () {if (Instance_ = = null) Instance_ = new ThreadPool ( 3); return instance_; } public synchronized void Destroypool () {for (int i = 0;i< m_idLethread.size (); i++) {Thread_inpool dthread = M_idlethread.get (i); Dthread.shutdown (); } for (int i = 0;i< m_activethread.size (); i++) {Thread_inpool dthread = M_activethread.get (i); Dthread.shutdown (); }} public synchronized void Inserttarget (java.lang.Runnable newtarget)//Insert New Task {if (newtarget = = Nu ll) return; if (!m_idlethread.isempty ()) {Thread_inpool i_thread = null; I_thread = M_idlethread.get (M_idlethread.size ()-1); M_idlethread.remove (I_thread); I_thread.settarget (Newtarget); M_activethread.add (I_thread); } else {m_runabletarger.add (newtarget); }} public synchronized int switchthread (Thread_inpool switchthread)//After the end of the task, if the task queue is not empty, continue with the new task, otherwise enter the empty queue { int rvalue = 0; if (!m_runabletarger.isempty ()) {java.lang.Runnable o_target = M_runabletarger.poll (); if (o_target! = null) {switchthread.settarget (o_target); Rvalue = 1; }} else { M_idlethread.add (Switchthread); M_activethread.remove (Switchthread); }return rvalue; }}
If there is any problem with the code, please correct it, related learning
Java-implemented thread pool