A threadpool with limited cache
As we all know ,we use the Executors.newCachedThreadPool() to create a thread pool which will use the SynchronousQueue to cache the not yet execute task.Then we will come to a problem:The cache queue will cache more and more Task waiting to be execute through the threadpool cann't execute the task in time.Oh,then we will get a exception :"OutOfMemory".That a terrible thing.
Then what can we do for that ? We are sure that should be stopped. When that case appearances, there must be something wrong happened to your system,but we cann't let it go.We can do just like this:
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author dengxiaoming E-mail: xiaoming.deng@renren.com
* @version createtime:2011-9-13 下午07:41:29
*
*/
public class ThreadPoolTest {
/**
* @param args
*/
public static void main(String[] args) {
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(5);
int end = 11;
final AtomicInteger count = new AtomicInteger();
ExecutorService service = new ThreadPoolExecutor(0,5,
60L, TimeUnit.SECONDS,
queue,
new MyThreadFactory(),
new MyRejectedExecutionHandler());
for(int i=0; i<end; i++) {
System.out.println("start i:" + i);
service.submit(new Runnable(){
public void run() {
try {
Thread.sleep(100);
System.out.println("0----------" + count.getAndIncrement());
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.println("it's over ....");
}
}
private static class MyThreadFactory implements ThreadFactory{
final ThreadGroup group;
final AtomicInteger threadNumber = new AtomicInteger(1);
final String namePrefix;
final String nameSuffix = "]";
final static String defaultThreadPoolName = "ThreadPool_Test_DefaultName";
public MyThreadFactory(){
this(defaultThreadPoolName);
}
public MyThreadFactory(String poolName) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "ThreadPool_Test_" + poolName + " _Pool [Thread-";
}
public MyThreadFactory(String poolName, ThreadGroup threadGroup) {
group = threadGroup;
namePrefix = "ThreadPool_Test_" + poolName + "_Pool [Thread-";
}
public ThreadGroup getThreadGroup() {
return group;
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group,
r,
namePrefix +
threadNumber.getAndIncrement() +
nameSuffix,
0);
t.setDaemon(true);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
private static class MyRejectedExecutionHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//log the exception for error analysis
System.out.println(new Date().toString() + "------ :reject the r.toString() " + r.toString());
throw new RejectedExecutionException();
}
}
}
Then after the waiting or running task number reached 10(5+5) you will get a RejectedExecution.
What should you do with those RejectedExecution is check your system and try to find out the reason .