1 |
/* |
2 |
* @(#)SingleThreadedExecutor.java |
3 |
*/ |
4 |
|
5 |
package java.util.concurrent; |
6 |
|
7 |
import java.util.List; |
8 |
|
9 |
/** |
10 |
* A {@link ThreadedExecutor} that runs tasks on a single background |
11 |
* thread. Tasks are executed sequentially in the order they were |
12 |
* submitted, with no more than one task executing at a time. |
13 |
* Generally, the tasks will all execute in the same thread, but if |
14 |
* this single thread terminates due to a failure during execution, a |
15 |
* new thread will take its place if needed to execute subsequent |
16 |
* tasks. |
17 |
* |
18 |
* @since 1.5 |
19 |
* |
20 |
* @spec JSR-166 |
21 |
* @revised $Date: 2003/05/14 21:30:48 $ |
22 |
* @editor $Author: tim $ |
23 |
*/ |
24 |
public class SingleThreadedExecutor implements ThreadedExecutor { |
25 |
|
26 |
private final ThreadedExecutor executor; |
27 |
|
28 |
/** |
29 |
* Creates a threaded executor that uses a single thread operating |
30 |
* off an unbounded queue. (Note however that if this single |
31 |
* thread terminates due to a failure during execution prior to |
32 |
* shutdown, a new one will take its place if needed to execute |
33 |
* subsequent tasks.) Tasks are guaranteed to execute |
34 |
* sequentially, and no more than one task will be active at any |
35 |
* given time. |
36 |
*/ |
37 |
public SingleThreadedExecutor() { |
38 |
executor = new ThreadPoolExecutor(1, 1, |
39 |
0L, TimeUnit.MILLISECONDS, |
40 |
new LinkedBlockingQueue()); |
41 |
} |
42 |
|
43 |
/* Executor implementation. Inherit javadoc from ThreadedExecutor. */ |
44 |
|
45 |
public void execute(Runnable command) { |
46 |
executor.execute(command); |
47 |
} |
48 |
|
49 |
/* ThreadedExecutor implementation. Inherit javadoc. */ |
50 |
|
51 |
public void setThreadFactory(ThreadFactory threadFactory) { |
52 |
executor.setThreadFactory(threadFactory); |
53 |
} |
54 |
|
55 |
public ThreadFactory getThreadFactory() { |
56 |
return executor.getThreadFactory(); |
57 |
} |
58 |
|
59 |
public void setCannotExecuteHandler(CannotExecuteHandler handler) { |
60 |
executor.setCannotExecuteHandler(handler); |
61 |
} |
62 |
|
63 |
public CannotExecuteHandler getCannotExecuteHandler() { |
64 |
return executor.getCannotExecuteHandler(); |
65 |
} |
66 |
|
67 |
public BlockingQueue getQueue() { |
68 |
return executor.getQueue(); |
69 |
} |
70 |
|
71 |
public void shutdown() { |
72 |
executor.shutdown(); |
73 |
} |
74 |
|
75 |
public List shutdownNow() { |
76 |
return executor.shutdownNow(); |
77 |
} |
78 |
|
79 |
public boolean isShutdown() { |
80 |
return executor.isShutdown(); |
81 |
} |
82 |
|
83 |
public void interrupt() { |
84 |
executor.interrupt(); |
85 |
} |
86 |
|
87 |
public boolean isTerminated() { |
88 |
return executor.isTerminated(); |
89 |
} |
90 |
|
91 |
public boolean awaitTermination(long timeout, TimeUnit granularity) |
92 |
throws InterruptedException { |
93 |
return executor.awaitTermination(timeout, granularity); |
94 |
} |
95 |
} |