A simple thread pool

  Abstract: A simple thread pool 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> this code from the "java game programming," Beijing hope that the electronic publishing.    David Brackeen the Qiuzhong Pan translation.    Thread Pool entry code.    Finishing head. 
  Import java.util.LinkedList; 

  Public class ThreadPool extends ThreadGroup (/ / thread as a group to achieve thread pool, innovative 

  <table Width="682" height="27" border="0"> <tr> <td width="407"> private boolean isAlive; / / this thread pool is activated 
  Private LinkedList taskQueue; / / storage tasks linked list 
  Private int threadID; / / thread pool threads of 
  Private static int threadPoolID; / / credited to the number of examples of a number of objects Thread Pool 

  / ** 
  The creation of a new thread pool. 
  @ Param numThreads pool of threads. 
  * / 
  Public ThreadPool (int numThreads) ( 
  Super ( "ThreadPool-" + (threadPoolID ++)); 
  SetDaemon (true); 

  IsAlive = true; 

  TaskQueue = new LinkedList (); 
  For (int i = 0; i    New PooledThread (). Start ();// start numThreads threads for the implementation of the tasks in the linked list 
  ) 
  ) 
  </ Td> <td width="251"> </ td> </ tr> </ table> 

  Public synchronized void runTask (Runnable task) (/ / add to the linked list of the measures 
  If (! IsAlive) ( 
  Throw new IllegalStateException (); 
  ) 
  If (task! = Null) ( 
  TaskQueue.add (task); / / add tasks 
  Notify ();// notice waiting threads 
  ) 

  ) 


  Protected synchronized Runnable getTask () / / from the linked list of tasks 
  Throws InterruptedException 
  ( 
  While (taskQueue.size () == 0) ( 
  If (! IsAlive) ( 
  Return null; 
  ) 
  Listless wait ();// If no task awaiting 
  ) 
  Return (Runnable) taskQueue.removeFirst ();// out tasks 
  ) 


  / ** 
  Close this thread pool and to return immediately.    Stop all threads, not wait for the performance of any task. 
  * / 
  Public synchronized void close () ( 
  If (isAlive) ( 
  IsAlive = false; 
  TaskQueue.clear (); 
  Interrupt (); 
  ) 
  ) 


  / ** 
  Close this thread pool, and wait for the completion of all the running thread, for the performance of any task. 
  * / 
  Public void join () ( 
  / / Notify all waiting for the thread, the thread is no longer activities 
  Synchronized (this) ( 
  IsAlive = false; 
  NotifyAll (); 
  ) 

  / / Wait for the completion of all threads 
  Thread [] threads = new Thread [activeCount ()]; 
  Int count = enumerate (threads); 
  For (int i = 0; i    Try ( 
  [I] threads. Join (); 
  ) 
  Catch (InterruptedException ex) () 
  ) 
  ) 


  / ** 
  PooledThread is in a thread pool threads, used to run tasks (Runnables). 
  * / 
  Private class PooledThread extends Thread (/ / private, can be an example of the internal 


  Public PooledThread () ( 
  Super (ThreadPool.this, 
  "PooledThread-" + (threadID ++)); 
  ) 


  Public void run () (/ / thread at the start of the implementation methods 
  While (! IsInterrupted ()) (/ / the key thread pool, the pool every thread as long as no interruption, recycling mandate 

  / / Get a task to run 
  Runnable task = null; 
  Try ( 
  Task = getTask ();// may need to wait 
  ) 
  Catch (InterruptedException ex) () 

  / / If getTask () returned null or was interrupted, 
  / / Close this thread by returning. 
  If (task == null) ( 
  Return; 
  ) 

  / / Run the task, and eat any exceptions it throws 
  Try ( 
  Task.run (); 
  ) 
  Catch (Throwable t) ( 
  UncaughtException (this, t); 
  ) / / Task disposed of 
  ) / / Thread to continue this cycle, or wait for the implementation of tasks. 
  ) 
  ) 
  ) 

  Following is the test procedure: 
  (Public class ThreadPoolTest 

  Public static void main (String [] args) ( 
  If (args.length! = 2) ( 
  System.out.println ( "Tests the ThreadPool task."); 
  System.out.println ( 
  "Usage: java ThreadPoolTest numTasks numThreads"); 
  System.out.println ( 
  "NumTasks - integer: number of task to run."); 
  System.out.println ( 
  "NumThreads - integer: number of threads" + 
  "In the thread pool."); 
  Return; 
  ) 
  Int numTasks = Integer.parseInt (args [0 ]);// need to run several tasks 
  Int numThreads = Integer.parseInt (args [1 ]);// thread pool launched several threads 

  / / Create the thread pool 
  ThreadPool threadPool = new ThreadPool (numThreads); / / generate the thread pool, start thread pool thread 

  / / Run example tasks 
  For (int i = 0; i    ThreadPool.runTask (createTask (i ));// mandate to increase the pool thread 
  ) 

  / / Closing pool and wait for the completion of all tasks. 
  ThreadPool.join (); 
  ) 


  / ** 
  Create simple task. 
  * / 
  Private static Runnable createTask (final int taskID) ( 
  Return new Runnable () ( 
  Public void run () ( 
  System.out.println ( "Task" taskID + + ": start"); 

  / / Simulate a long-running task 
  Try ( 
  Thread.sleep (500); 
  ) 
  Catch (InterruptedException ex) () 

  System.out.println ( "Task" taskID + + ": end"); 
  ) 
  ); 
  ) 

  ) 
  Run: 

  C: \ java> java ThreadPoolTest 8 4 
  Task 0: start 
  Task 1: start 
  Task 2: start 
  Task 3: start 
  Task 0: end 
  Task 4: start 
  Task 2: end 
  Task 5: start 
  Task 3: end 
  Task 6: start 
  Task 1: end 
  Task 7: start 
  Task 4: end 
  Task 5: end 
  Task 6: end 
  Task 7: end 

  C: \ java> function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( " CommentXMLStore "); document.getElementById (ElementID). CommentsPersistDiv.getAttribute value = (" CommentContent ");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply