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 | 29 |
| 30 |
Tags
- history
- ubuntu
- Italy
- Malaysia
- QT
- essay
- erlang
- programming_book
- Java
- django
- France
- RFID
- Kuala Lumpur
- Artificial Intelligence
- MySQL
- comic agile
- program
- Linux
- web
- Software Engineering
- Book
- Programming
- management
- leadership
- agile
- Book review
- AI
- Python
- hbase
- hadoop
Archives
- Today
- Total
ThreadPoolExecutor basic example 2 본문
// http://bobah.net/d4d/source-code/misc/thread-pool-executor-example-j2ee
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestThreadPoolExecutor2 {
private static class RejectedHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable arg0, ThreadPoolExecutor arg1) {
// TODO Auto-generated method stub
System.err.println(Thread.currentThread().getName() +
" execution rejected: " + arg0);
}
}
private static class Task implements Runnable {
private static SimpleDateFormat fmt =
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
private String name;
private Date created;
public Task(String name) {
this.name = name;
this.created = new Date();
}
@Override
public void run() {
final boolean wantOverflow = true;
System.out.println(Thread.currentThread().getName() +
" executing " + this);
try {
Thread.sleep(wantOverflow ? 50 : 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +
" executed " + this);
}
@Override
public String toString() {
return name + ", created " + fmt.format(created);
}
}
public static void main(String[] args) throws InterruptedException {
final boolean wantExceptionOnReject = false;
// fixed pool, unlimited queue
// ExecutorService service = Executors.newFixedThreadPool(10 /* size */);
// ThreadPoolExecutor executor = (ThreadPoolExecutor) service;
// fixed pool fixed queue
BlockingQueue queue =
new ArrayBlockingQueue(100, true);
ThreadPoolExecutor executor = new ThreadPoolExecutor(
10, // core size
20, // max size
1, // keep alive time
TimeUnit.MINUTES, // keep alive time units
queue // the queue to use
);
// set rejected execution handler
// or catch exception from executor.execute (see below)
if (!wantExceptionOnReject)
executor.setRejectedExecutionHandler(new RejectedHandler());
for(long i = 0; ; ++i) {
Task t = new Task(String.valueOf(i));
System.out.println(Thread.currentThread().getName() + " submitted " +
t + ", queue size = " + executor.getQueue().size());
try {
executor.execute(t);
} catch (RejectedExecutionException e) {
// will be thrown if rejected execution handler
// is not set with executor.setRejectedExecutionHandler
e.printStackTrace();
}
Thread.sleep(1);
}
}
}
Comments