1 21 package net.sf.hajdbc.util.concurrent; 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.concurrent.Callable ; 28 import java.util.concurrent.ExecutionException ; 29 import java.util.concurrent.ExecutorService ; 30 import java.util.concurrent.Future ; 31 import java.util.concurrent.TimeUnit ; 32 33 38 public class SynchronousExecutor implements ExecutorService 39 { 40 private boolean shutdown = false; 41 42 45 public void shutdown() 46 { 47 this.shutdown = true; 48 } 49 50 53 public List <Runnable > shutdownNow() 54 { 55 this.shutdown(); 56 57 return Collections.emptyList(); 58 } 59 60 63 public boolean isShutdown() 64 { 65 return this.shutdown; 66 } 67 68 71 public boolean isTerminated() 72 { 73 return this.isShutdown(); 74 } 75 76 79 public boolean awaitTermination(long timeout, TimeUnit unit) 80 { 81 return true; 82 } 83 84 87 public <T> Future <T> submit(Callable <T> task) 88 { 89 return new SynchronousFuture<T>(task); 90 } 91 92 95 public <T> Future <T> submit(Runnable task, T result) 96 { 97 return new SynchronousFuture<T>(new CallableTask<T>(task, result)); 98 } 99 100 103 public Future <?> submit(Runnable task) 104 { 105 return new SynchronousFuture<Void >(new CallableTask<Void >(task, null)); 106 } 107 108 111 public <T> List <Future <T>> invokeAll(Collection <Callable <T>> tasks) 112 { 113 List <Future <T>> futureList = new ArrayList <Future <T>>(tasks.size()); 114 115 for (Callable <T> task: tasks) 116 { 117 futureList.add(new SynchronousFuture<T>(task)); 118 } 119 120 return futureList; 121 } 122 123 126 public <T> List <Future <T>> invokeAll(Collection <Callable <T>> tasks, long timeout, TimeUnit unit) 127 { 128 return this.invokeAll(tasks); 129 } 130 131 134 public <T> T invokeAny(Collection <Callable <T>> tasks) throws ExecutionException 135 { 136 Throwable exception = null; 137 138 for (Callable <T> task: tasks) 139 { 140 try 141 { 142 return task.call(); 143 } 144 catch (Throwable e) 145 { 146 exception = e; 147 } 148 } 149 150 throw new ExecutionException (exception); 151 } 152 153 156 public <T> T invokeAny(Collection <Callable <T>> tasks, long timeout, TimeUnit unit) throws ExecutionException 157 { 158 return this.invokeAny(tasks); 159 } 160 161 164 public void execute(Runnable task) 165 { 166 task.run(); 167 } 168 169 private class SynchronousFuture<T> implements Future <T> 170 { 171 private T result = null; 172 private Throwable exception = null; 173 174 public SynchronousFuture(Callable <T> task) 175 { 176 try 177 { 178 this.result = task.call(); 179 } 180 catch (Exception e) 181 { 182 this.exception = e; 183 } 184 } 185 186 189 public boolean cancel(boolean mayInterruptIfRunning) 190 { 191 return false; 192 } 193 194 197 public boolean isCancelled() 198 { 199 return false; 200 } 201 202 205 public boolean isDone() 206 { 207 return true; 208 } 209 210 213 public T get() throws ExecutionException 214 { 215 if (this.exception != null) 216 { 217 throw new ExecutionException (this.exception); 218 } 219 220 return this.result; 221 } 222 223 226 public T get(long timeout, TimeUnit unit) throws ExecutionException 227 { 228 return this.get(); 229 } 230 } 231 232 private class CallableTask<T> implements Callable <T> 233 { 234 private Runnable task; 235 private T result; 236 237 public CallableTask(Runnable task, T result) 238 { 239 this.task = task; 240 this.result = result; 241 } 242 243 246 public T call() throws Exception 247 { 248 this.task.run(); 249 250 return this.result; 251 } 252 } 253 } 254 | Popular Tags |