KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > util > concurrent > SynchronousExecutor


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.util.concurrent;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.concurrent.Callable JavaDoc;
28 import java.util.concurrent.ExecutionException JavaDoc;
29 import java.util.concurrent.ExecutorService JavaDoc;
30 import java.util.concurrent.Future JavaDoc;
31 import java.util.concurrent.TimeUnit JavaDoc;
32
33 /**
34  * Executor service that executes tasks in the caller thread.
35  *
36  * @author Paul Ferraro
37  */

38 public class SynchronousExecutor implements ExecutorService JavaDoc
39 {
40     private boolean shutdown = false;
41     
42     /**
43      * @see java.util.concurrent.ExecutorService#shutdown()
44      */

45     public void shutdown()
46     {
47         this.shutdown = true;
48     }
49
50     /**
51      * @see java.util.concurrent.ExecutorService#shutdownNow()
52      */

53     public List JavaDoc<Runnable JavaDoc> shutdownNow()
54     {
55         this.shutdown();
56         
57         return Collections.emptyList();
58     }
59
60     /**
61      * @see java.util.concurrent.ExecutorService#isShutdown()
62      */

63     public boolean isShutdown()
64     {
65         return this.shutdown;
66     }
67
68     /**
69      * @see java.util.concurrent.ExecutorService#isTerminated()
70      */

71     public boolean isTerminated()
72     {
73         return this.isShutdown();
74     }
75
76     /**
77      * @see java.util.concurrent.ExecutorService#awaitTermination(long, java.util.concurrent.TimeUnit)
78      */

79     public boolean awaitTermination(long timeout, TimeUnit JavaDoc unit)
80     {
81         return true;
82     }
83
84     /**
85      * @see java.util.concurrent.ExecutorService#submit(java.util.concurrent.Callable)
86      */

87     public <T> Future JavaDoc<T> submit(Callable JavaDoc<T> task)
88     {
89         return new SynchronousFuture<T>(task);
90     }
91
92     /**
93      * @see java.util.concurrent.ExecutorService#submit(java.lang.Runnable, Object)
94      */

95     public <T> Future JavaDoc<T> submit(Runnable JavaDoc task, T result)
96     {
97         return new SynchronousFuture<T>(new CallableTask<T>(task, result));
98     }
99
100     /**
101      * @see java.util.concurrent.ExecutorService#submit(java.lang.Runnable)
102      */

103     public Future JavaDoc<?> submit(Runnable JavaDoc task)
104     {
105         return new SynchronousFuture<Void JavaDoc>(new CallableTask<Void JavaDoc>(task, null));
106     }
107
108     /**
109      * @see java.util.concurrent.ExecutorService#invokeAll(java.util.Collection)
110      */

111     public <T> List JavaDoc<Future JavaDoc<T>> invokeAll(Collection JavaDoc<Callable JavaDoc<T>> tasks)
112     {
113         List JavaDoc<Future JavaDoc<T>> futureList = new ArrayList JavaDoc<Future JavaDoc<T>>(tasks.size());
114         
115         for (Callable JavaDoc<T> task: tasks)
116         {
117             futureList.add(new SynchronousFuture<T>(task));
118         }
119         
120         return futureList;
121     }
122
123     /**
124      * @see java.util.concurrent.ExecutorService#invokeAll(java.util.Collection, long, java.util.concurrent.TimeUnit)
125      */

126     public <T> List JavaDoc<Future JavaDoc<T>> invokeAll(Collection JavaDoc<Callable JavaDoc<T>> tasks, long timeout, TimeUnit JavaDoc unit)
127     {
128         return this.invokeAll(tasks);
129     }
130
131     /**
132      * @see java.util.concurrent.ExecutorService#invokeAny(java.util.Collection)
133      */

134     public <T> T invokeAny(Collection JavaDoc<Callable JavaDoc<T>> tasks) throws ExecutionException JavaDoc
135     {
136         Throwable JavaDoc exception = null;
137         
138         for (Callable JavaDoc<T> task: tasks)
139         {
140             try
141             {
142                 return task.call();
143             }
144             catch (Throwable JavaDoc e)
145             {
146                 exception = e;
147             }
148         }
149         
150         throw new ExecutionException JavaDoc(exception);
151     }
152
153     /**
154      * @see java.util.concurrent.ExecutorService#invokeAny(java.util.Collection, long, java.util.concurrent.TimeUnit)
155      */

156     public <T> T invokeAny(Collection JavaDoc<Callable JavaDoc<T>> tasks, long timeout, TimeUnit JavaDoc unit) throws ExecutionException JavaDoc
157     {
158         return this.invokeAny(tasks);
159     }
160
161     /**
162      * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
163      */

164     public void execute(Runnable JavaDoc task)
165     {
166         task.run();
167     }
168     
169     private class SynchronousFuture<T> implements Future JavaDoc<T>
170     {
171         private T result = null;
172         private Throwable JavaDoc exception = null;
173
174         public SynchronousFuture(Callable JavaDoc<T> task)
175         {
176             try
177             {
178                 this.result = task.call();
179             }
180             catch (Exception JavaDoc e)
181             {
182                 this.exception = e;
183             }
184         }
185         
186         /**
187          * @see java.util.concurrent.Future#cancel(boolean)
188          */

189         public boolean cancel(boolean mayInterruptIfRunning)
190         {
191             return false;
192         }
193
194         /**
195          * @see java.util.concurrent.Future#isCancelled()
196          */

197         public boolean isCancelled()
198         {
199             return false;
200         }
201
202         /**
203          * @see java.util.concurrent.Future#isDone()
204          */

205         public boolean isDone()
206         {
207             return true;
208         }
209
210         /**
211          * @see java.util.concurrent.Future#get()
212          */

213         public T get() throws ExecutionException JavaDoc
214         {
215             if (this.exception != null)
216             {
217                 throw new ExecutionException JavaDoc(this.exception);
218             }
219             
220             return this.result;
221         }
222
223         /**
224          * @see java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)
225          */

226         public T get(long timeout, TimeUnit JavaDoc unit) throws ExecutionException JavaDoc
227         {
228             return this.get();
229         }
230     }
231     
232     private class CallableTask<T> implements Callable JavaDoc<T>
233     {
234         private Runnable JavaDoc task;
235         private T result;
236         
237         public CallableTask(Runnable JavaDoc task, T result)
238         {
239             this.task = task;
240             this.result = result;
241         }
242         
243         /**
244          * @see java.util.concurrent.Callable#call()
245          */

246         public T call() throws Exception JavaDoc
247         {
248             this.task.run();
249             
250             return this.result;
251         }
252     }
253 }
254
Popular Tags