ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executors.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/Executors.java (file contents):
Revision 1.1 by tim, Wed May 14 21:30:46 2003 UTC vs.
Revision 1.2 by dl, Tue May 27 18:14:40 2003 UTC

# Line 1 | Line 1
1   /*
2 < * @(#)Executors.java
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain. Use, modify, and
4 > * redistribute this code in any way without acknowledgement.
5   */
6  
7   package java.util.concurrent;
8 + import java.util.*;
9  
10   /**
11 < * A factory for the <tt>Executor</tt> classes defined in
12 < * <tt>java.util.concurrent</tt>.
11 > * Factory and utility methods for the <tt>Executor</tt> classes
12 > * defined in <tt>java.util.concurrent</tt>.
13   *
14   * <p>An Executor is a framework for executing Runnables.  The
15   * Executor manages queueing and scheduling of tasks, and creation and
# Line 17 | Line 20 | package java.util.concurrent;
20   *
21   * @since 1.5
22   * @see Executor
23 < * @see ThreadedExecutor
23 > * @see ExecutorService
24   * @see Future
25   *
26   * @spec JSR-166
# Line 27 | Line 30 | package java.util.concurrent;
30   public class Executors {
31  
32      /**
33 +     * A wrapper class that exposes only the ExecutorService methods
34 +     * of an implementation.
35 +     */
36 +    static private class DelegatedExecutorService implements ExecutorService {
37 +        private final ExecutorService e;
38 +        DelegatedExecutorService(ExecutorService executor) { e = executor; }
39 +        public void execute(Runnable command) { e.execute(command); }
40 +        public void shutdown() { e.shutdown(); }
41 +        public List shutdownNow() { return e.shutdownNow(); }
42 +        public boolean isShutdown() { return e.isShutdown(); }
43 +        public boolean isTerminated() { return e.isTerminated(); }
44 +        public boolean awaitTermination(long timeout, TimeUnit unit)
45 +            throws InterruptedException {
46 +            return e.awaitTermination(timeout, unit);
47 +        }
48 +    }
49 +
50 +    /**
51       * Creates a thread pool that reuses a fixed set of threads
52       * operating off a shared unbounded queue.
53       *
54       * @param nThreads the number of threads in the pool
55       * @return the newly created thread pool
56       */
57 <    public static ThreadedExecutor newFixedThreadPool(int nThreads) {
58 <        return new ThreadPoolExecutor(nThreads, nThreads,
59 <                                      0L, TimeUnit.MILLISECONDS,
60 <                                      new LinkedBlockingQueue());
57 >    public static ExecutorService newFixedThreadPool(int nThreads) {
58 >        return new DelegatedExecutorService
59 >            (new ThreadPoolExecutor(nThreads, nThreads,
60 >                                    0L, TimeUnit.MILLISECONDS,
61 >                                    new LinkedBlockingQueue()));
62 >    }
63 >
64 >    /**
65 >     * Creates a thread pool that reuses a fixed set of threads
66 >     * operating off a shared unbounded queue, using the provided
67 >     * ThreadFactory to create new threads when needed.
68 >     *
69 >     * @param nThreads the number of threads in the pool
70 >     * @param threadfactory the factory to use when creating new threads
71 >     * @return the newly created thread pool
72 >     */
73 >    public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
74 >        return new DelegatedExecutorService
75 >            (new ThreadPoolExecutor(nThreads, nThreads,
76 >                                    0L, TimeUnit.MILLISECONDS,
77 >                                    new LinkedBlockingQueue(),
78 >                                    threadFactory, null));
79 >    }
80 >
81 >    /**
82 >     * Creates an Executor that uses a single worker thread operating
83 >     * off an unbounded queue. (Note however that if this single
84 >     * thread terminates due to a failure during execution prior to
85 >     * shutdown, a new one will take its place if needed to execute
86 >     * subsequent tasks.)  Tasks are guaranteed to execute
87 >     * sequentially, and no more than one task will be active at any
88 >     * given time.
89 >     *
90 >     * @return the newly-created single-threaded Executor
91 >     */
92 >    public static ExecutorService newSingleThreadExecutor() {
93 >        return new DelegatedExecutorService
94 >            (new ThreadPoolExecutor(1, 1,
95 >                                    0L, TimeUnit.MILLISECONDS,
96 >                                    new LinkedBlockingQueue()));
97 >    }
98 >
99 >    /**
100 >     * Creates an Executor that uses a single worker thread operating
101 >     * off an unbounded queue, and uses the provided ThreadFactory to
102 >     * create new threads when needed.
103 >     * @param threadfactory the factory to use when creating new
104 >     * threads
105 >     *
106 >     * @return the newly-created single-threaded Executor
107 >     */
108 >    public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
109 >        return new DelegatedExecutorService
110 >            (new ThreadPoolExecutor(1, 1,
111 >                                    0L, TimeUnit.MILLISECONDS,
112 >                                    new LinkedBlockingQueue(),
113 >                                    threadFactory, null));
114      }
115  
116      /**
# Line 53 | Line 127 | public class Executors {
127       *
128       * @return the newly created thread pool
129       */
130 <    public static ThreadedExecutor newCachedThreadPool() {
131 <        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
132 <                                      60000, TimeUnit.MILLISECONDS,
133 <                                      new SynchronousQueue());
130 >    public static ExecutorService newCachedThreadPool() {
131 >        return new DelegatedExecutorService
132 >            (new ThreadPoolExecutor(0, Integer.MAX_VALUE,
133 >                                    60, TimeUnit.SECONDS,
134 >                                    new SynchronousQueue()));
135      }
136  
137      /**
138 <     * Creates a thread pool that reuses a limited pool of cached
139 <     * threads.
140 <     *
141 <     * @param minThreads the minimum number of threads to keep in the
142 <     * pool, even if they are idle.
68 <     * @param maxThreads the maximum number of threads to allow in the
69 <     * pool.
70 <     * @param keepAliveTime when the number of threads is greater than
71 <     * the minimum, this is the maximum time that excess idle threads
72 <     * will wait for new tasks before terminating.
73 <     * @param granularity the time unit for the keepAliveTime
74 <     * argument.
75 <     * @param queue the queue to use for holding tasks before they
76 <     * are executed. This queue will hold only the <tt>Runnable</tt>
77 <     * tasks submitted by the <tt>execute</tt> method.
138 >     * Creates a thread pool that creates new threads as needed, but
139 >     * will reuse previously constructed threads when they are
140 >     * available, and uses the provided
141 >     * ThreadFactory to create new threads when needed.
142 >     * @param threadfactory the factory to use when creating new threads
143       * @return the newly created thread pool
79     * @throws IllegalArgumentException if minThreads, maxThreads, or
80     * keepAliveTime less than zero, or if minThreads greater than
81     * maxThreads.  
82     * @throws NullPointerException if queue is null
83     */
84    public static ThreadedExecutor newThreadPool(int minThreads,
85                                                   int maxThreads,
86                                                   long keepAliveTime,
87                                                   TimeUnit granularity,
88                                                   BlockingQueue queue) {
89        return new ThreadPoolExecutor(minThreads, maxThreads,
90                                      keepAliveTime, granularity,
91                                      queue);
92    }
93
94    /**
95     * Creates an Executor that uses a single worker thread operating
96     * off an unbounded queue. (Note however that if this single
97     * thread terminates due to a failure during execution prior to
98     * shutdown, a new one will take its place if needed to execute
99     * subsequent tasks.)  Tasks are guaranteed to execute
100     * sequentially, and no more than one task will be active at any
101     * given time.
102     *
103     * @return the newly-created single-threaded Executor
144       */
145 <    public static SingleThreadedExecutor newSingleThreadExecutor() {
146 <        return new SingleThreadedExecutor();
145 >    public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
146 >        return new DelegatedExecutorService
147 >            (new ThreadPoolExecutor(0, Integer.MAX_VALUE,
148 >                                    60, TimeUnit.SECONDS,
149 >                                    new SynchronousQueue(),
150 >                                    threadFactory, null));
151      }
152  
153      /**
# Line 118 | Line 162 | public class Executors {
162       * @param keepAliveTime when the number of threads is greater than
163       * the minimum, this is the maximum time that excess idle threads
164       * will wait for new tasks before terminating.
165 <     * @param granularity the time unit for the keepAliveTime
165 >     * @param unit the time unit for the keepAliveTime
166       * argument.
167       * @return the newly created ScheduledExecutor
168       */
169 <    public static ScheduledExecutor newScheduledExecutor(int minThreads,
170 <                                                         int maxThreads,
171 <                                                         long keepAliveTime,
172 <                                                         TimeUnit granularity) {
173 <        return new ScheduledExecutor(minThreads, maxThreads,
174 <                                     keepAliveTime, granularity);
175 <    }
169 >    //    public static ScheduledExecutor newScheduledExecutor(int minThreads,
170 >    //                                                         int maxThreads,
171 >    //                                                         long keepAliveTime,
172 >    //                                                         TimeUnit unit) {
173 >    //        return new ScheduledExecutor(minThreads, maxThreads,
174 >    //                                     keepAliveTime, unit);
175 >    //    }
176  
177      /**
178       * Executes a Runnable task and returns a Future representing that

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines