1 package org.jacorb.notification.engine; 2 3 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 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 name; 45 46 private DefaultThreadFactory(String name) 47 { 48 super(); 49 this.name = name; 50 } 51 52 public synchronized Thread newThread(Runnable task) 53 { 54 Thread _thread = new Thread (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 73 public static TaskExecutor getDefaultExecutor() 74 { 75 return DIRECT_EXECUTOR; 76 } 77 78 80 public DefaultTaskExecutor(final String name, int numberOfThreads) 81 { 82 this(name, numberOfThreads, false); 83 } 84 85 public DefaultTaskExecutor(final String name, int numberOfThreads, boolean mayDie) 86 { 87 if (numberOfThreads < 0) 88 { 89 throw new IllegalArgumentException (); 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 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 r) throws InterruptedException 143 { 144 executor_.execute(r); 145 } 146 } 147 148 | Popular Tags |