KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > nio > dispatcher > ThreadedDispatcher


1 package org.sapia.ubik.net.nio.dispatcher;
2
3 import java.io.IOException JavaDoc;
4
5 import org.sapia.ubik.net.nio.Cycle;
6 import org.sapia.ubik.net.nio.Dispatcher;
7
8 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
9 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
10
11 /**
12  * This class implements a dispatcher that performs read, write and process
13  * operations in separate threads (that are pooled).
14  * <p>
15  * There is thus a single pool of threads that is used for all types of
16  * operations.
17  *
18  * @author Yanick Duchesne
19  *
20  * <dl>
21  * <dt><b>Copyright: </b>
22  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
23  * Source Software </a>. All Rights Reserved.</dd>
24  * </dt>
25  * <dt><b>License: </b>
26  * <dd>Read the license.txt file of the jar or visit the <a
27  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
28  * OSS web site</dd>
29  * </dt>
30  * </dl>
31  */

32 public class ThreadedDispatcher implements Dispatcher {
33
34   private PooledExecutor _exec;
35
36   public ThreadedDispatcher(){
37     _exec = new PooledExecutor(new LinkedQueue());
38   }
39   public ThreadedDispatcher(PooledExecutor exec) {
40     _exec = exec;
41   }
42
43   /**
44    * @see org.sapia.ubik.net.nio.Dispatcher#dispatch(org.sapia.ubik.net.nio.Cycle)
45    */

46   public void dispatch(Cycle cycle) throws IOException JavaDoc {
47     try {
48       _exec.execute(new CycleCommand().init(cycle));
49     } catch(Exception JavaDoc e) {
50       cycle.error(e);
51       cycle.next();
52     }
53   }
54
55   /**
56    * @see org.sapia.ubik.net.nio.Dispatcher#close()
57    */

58   public void close() {
59     _exec.shutdownAfterProcessingCurrentlyQueuedTasks();
60   }
61 }
62
Popular Tags