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
| import java.util.Date; import java.util.concurrent.*; public class ObjectTest { public static void main(String[] args) { Future future = null; ExecutorService executorService = Executors.newFixedThreadPool(5); for (int i = 0; i < 10; i++) { Runnable runnable = new Runnable() { @Override public void run() { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService; int poolSize = threadPoolExecutor.getPoolSize(); String threadName = Thread.currentThread().getName(); System.out.println("[총 스레드 개수:" + poolSize + "] 작업 스레드 이름: " + threadName);
try { Thread.sleep(5000); System.out.println(new Date().toString()); } catch (InterruptedException e) { e.printStackTrace(); } } };
future = executorService.submit(runnable); } try { future.get(); System.out.println("[작업 처리 완료]"); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } executorService.shutdown(); System.out.println("Finish"); } }
|