Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- programming_book
- Spain
- program
- Java
- essay
- Book review
- MySQL
- Italy
- Book
- Programming
- agile
- comic agile
- psychology
- hbase
- Kuala Lumpur
- web
- history
- Linux
- Python
- ubuntu
- France
- QT
- RFID
- erlang
- leadership
- Malaysia
- hadoop
- management
- Software Engineering
- django
Archives
- Today
- Total
ThreadPoolExecutor basic example 본문
// http://programmingexamples.wikidot.com/threadpoolexecutor import java.util.concurrent.*; import java.util.*; class TestThreadPoolExecutor { int poolSize = 2; int maxPoolSize = 2; long keepAliveTime = 10; ThreadPoolExecutor threadPool = null; final ArrayBlockingQueuequeue = new ArrayBlockingQueue (5); public TestThreadPoolExecutor() { threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue); } public void runTask(Runnable task) { // System.out.println("Task count.."+threadPool.getTaskCount() ); // System.out.println("Queue Size before assigning the // task.."+queue.size() ); threadPool.execute(task); // System.out.println("Queue Size after assigning the // task.."+queue.size() ); // System.out.println("Pool Size after assigning the // task.."+threadPool.getActiveCount() ); // System.out.println("Task count.."+threadPool.getTaskCount() ); System.out.println("Task count.." + queue.size()); } public void shutDown() { threadPool.shutdown(); } public static void main(String args[]) { TestThreadPoolExecutor mtpe = new TestThreadPoolExecutor(); mtpe.runTask(new TestThread("First Task", 1000)); mtpe.runTask(new TestThread("Second Task", 1000)); mtpe.runTask(new TestThread("Third Task", 1000)); mtpe.runTask(new TestThread("Fourth Task", 1000)); mtpe.runTask(new TestThread("Fifth Task", 1000)); mtpe.runTask(new TestThread("Sixth Task", 1000)); mtpe.shutDown(); } } class TestThread implements Runnable { private String msg; private int time; public TestThread(String msg, int time) { this.msg = msg; this.time = time; } public void run() { for (int i = 0; i < 10; i++) { try { System.out.println(msg); Thread.sleep(time); } catch (InterruptedException ie) { } } } }
Comments