1. Thread Pool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import java.time.LocalDateTime; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public class ThreadPool<T> { ExecutorService executorService; CompletionService<T> completionService; public void createFixedThreadPool(int nThreads) { createService(nThreads); } public void createFixedThreadPool() { createService(Runtime.getRuntime().availableProcessors()); } private void createService(int nThreads) { executorService = Executors.newFixedThreadPool(nThreads); completionService = new ExecutorCompletionService<T>(executorService); } public void shutdown() { executorService.shutdown(); } public List<Runnable> shutdownNow() { return executorService.shutdownNow(); } public Boolean awaitTermination( long timeout, TimeUnit unit ) throws InterruptedException { return executorService.awaitTermination(timeout, unit); } public void execute(Runnable task) { executorService.execute(task); } public Future<Result> submit(Runnable task, Result result) { return executorService.submit(task, result); } public Future<T> submit(Callable<T> task) { return executorService.submit(task); } public Future<T> submitCompletion(Callable<T> task) { return completionService.submit(task); } //---------------------------------------------------------------------- // Test Code - Start //---------------------------------------------------------------------- public static Runnable newRunnable(int id) { Runnable task = new Runnable() { @Override public void run() { String threadName = Thread.currentThread().getName(); for( int i=0; i < 10; i++ ) { try { Thread.sleep(1000); System.out.println( "[" + threadName + "] Runnable_" + id + ": " + i); } catch (InterruptedException e) { e.printStackTrace(); } } } }; return task; } public static Callable<Integer> newCallable(int id) { Callable<Integer> task = new Callable<Integer>() { @Override public Integer call() throws Exception { String threadName = Thread.currentThread().getName(); for( int i=0; i < 10; i++ ) { try { Thread.sleep(1000); System.out.println( "[" + threadName + "] Callable_" + id + ": " + i); } catch (InterruptedException e) { e.printStackTrace(); } } return id*100; } }; return task; } public static void sleep(int sec) { try { Thread.sleep(sec*1000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void println(String s) { System.out.println( "[" + LocalDateTime.now() + "] " + s); } public static void runnableTest() { println("runnableTest() start !"); ThreadPool<Integer> threads = new ThreadPool<Integer>(); threads.createFixedThreadPool(); for( int i=0; i < 10; i++ ) { threads.execute(newRunnable(i)); } println("runnableTest() shutdown !"); threads.shutdown(); println("runnableTest() end !"); } @SuppressWarnings("unchecked") public static void callableTest() { println("callableTest() start !"); ThreadPool<Integer> threads = new ThreadPool<Integer>(); threads.createFixedThreadPool(); Future<Integer>[] future = new Future[10]; for( int i=0; i < 10; i++ ) { future[i] = threads.submit(newCallable(i)); } println("callableTest() shutdown !"); threads.shutdown(); Integer[] result = new Integer[10]; for( int i=0; i < 10; i++ ) { try { result[i] = (Integer)future[i].get(); println("callableTest() result[" + i + "] = " + result[i]); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } println("callableTest() end !"); } public static void callableWithCompletionServiceTest() { println("callableWithCompletionServiceTest() start !"); ThreadPool<Integer> threads = new ThreadPool<Integer>(); threads.createFixedThreadPool(); for( int i=0; i < 10; i++ ) { threads.submitCompletion(newCallable(i)); } println("callableWithCompletionServiceTest() shutdown !"); threads.shutdown(); for( int i=0; i < 10; i++ ) { try { Future<Integer> future = threads.completionService.take(); Integer result = (Integer)future.get(); println("callableWithCompletionServiceTest() result[" + i + "] = " + result); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } println("callableWithCompletionServiceTest() end !"); } public static void main(String[] args) { runnableTest(); callableTest(); callableWithCompletionServiceTest(); } } |
댓글 없음:
댓글 쓰기