KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > engine > DefaultTaskExecutor


1 package org.jacorb.notification.engine;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import org.jacorb.notification.interfaces.Disposable;
25 import org.jacorb.notification.util.DisposableManager;
26
27 import EDU.oswego.cs.dl.util.concurrent.DirectExecutor;
28 import EDU.oswego.cs.dl.util.concurrent.Executor;
29 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
30 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
31 import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;
32
33 /**
34  * @author Alphonse Bendt
35  * @version $Id: DefaultTaskExecutor.java,v 1.1 2005/02/14 00:03:09 alphonse.bendt Exp $
36  */

37
38 public class DefaultTaskExecutor implements TaskExecutor
39 {
40     private static final class DefaultThreadFactory implements ThreadFactory
41     {
42         private int counter_ = 0;
43
44         private final String JavaDoc name;
45
46         private DefaultThreadFactory(String JavaDoc name)
47         {
48             super();
49             this.name = name;
50         }
51
52         public synchronized Thread JavaDoc newThread(Runnable JavaDoc task)
53         {
54             Thread JavaDoc _thread = new Thread JavaDoc(task);
55
56             _thread.setDaemon(true);
57             _thread.setName(name + "#" + (counter_++));
58
59             return _thread;
60         }
61     }
62
63     private static final DefaultTaskExecutor DIRECT_EXECUTOR = new DefaultTaskExecutor("Direct", 0);
64
65     private final Executor executor_;
66
67     private final DisposableManager disposeHooks_ = new DisposableManager();
68     
69     private LinkedQueue channel_;
70
71     ////////////////////////////////////////
72

73     public static TaskExecutor getDefaultExecutor()
74     {
75         return DIRECT_EXECUTOR;
76     }
77     
78     ////////////////////////////////////////
79

80     public DefaultTaskExecutor(final String JavaDoc name, int numberOfThreads)
81     {
82         this(name, numberOfThreads, false);
83     }
84
85     public DefaultTaskExecutor(final String JavaDoc name, int numberOfThreads, boolean mayDie)
86     {
87         if (numberOfThreads < 0)
88         {
89             throw new IllegalArgumentException JavaDoc();
90         }
91         else if (numberOfThreads == 0)
92         {
93             executor_ = new DirectExecutor();
94         }
95         else
96         {
97             ThreadFactory _threadFactory = new DefaultThreadFactory(name);
98
99             channel_ = new LinkedQueue();
100
101             PooledExecutor _executor = new PooledExecutor(channel_);
102
103             _executor.setThreadFactory(_threadFactory);
104             if (!mayDie)
105             {
106                 _executor.setKeepAliveTime(-1);
107             }
108             _executor.createThreads(numberOfThreads);
109
110             executor_ = _executor;
111         }
112     }
113
114     ////////////////////////////////////////
115

116     public boolean isTaskQueued()
117     {
118         if (channel_ != null)
119         {
120             return !channel_.isEmpty();
121         }
122
123         return false;
124     }
125
126     public void dispose()
127     {
128         if (executor_ instanceof PooledExecutor)
129         {
130             ((PooledExecutor) executor_).shutdownNow();
131             ((PooledExecutor) executor_).interruptAll();
132         }
133         
134         disposeHooks_.dispose();
135     }
136
137     public void addDisposeHook(Disposable d)
138     {
139         disposeHooks_.addDisposable(d);
140     }
141     
142     public void execute(Runnable JavaDoc r) throws InterruptedException JavaDoc
143     {
144         executor_.execute(r);
145     }
146 }
147
148
Popular Tags