Java ThreadPool Test Sample Program
예전에는 직접 만들어 사용하시는분들이 많았을겁니다.. 그러나 이제는 아래와 같이 사용하면 그만 ~
package com.mulder.util.tester;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTester {
// 3 이라는 파라미터가 ThreadPool Size 를 지정한다.
private static final ExecutorService threadPool = Executors.newFixedThreadPool(3);
public static void initTest() {
}
public static void destroyTester() {
threadPool.shutdown();
}
// 의미없는 쓰레드의 테스트처리용.
private static int gidx = 0;
private String[] arrText = {"111","222","333","444"};
public void process(String request) {
threadPool.execute(new Runnable() {
public void run() {
Random r = new Random();
System.out.println(++gidx + ":myId:" + Thread.currentThread().getId() );
//System.out.println("arrText:" + ( Math.abs(r.nextInt()) % arrText.length ) );
System.out.println("arrText:" + arrText[ Math.abs(r.nextInt()) % arrText.length ] );
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0; i<1000; i++) {
// 테스트 쓰레드 1000개를 만들어서 콜한다.
new Thread(new Runnable() { public void run() {
ThreadPoolTester t = new ThreadPoolTester();
t.process("system");
} }).start();
;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ThreadPoolTester.destroyTester();
}
}