Thursday, January 6, 2011

Code Example to use ThreadPoolExecutor in Java

// to create Thread Pooling use
private ThreadPoolExecutor pool=null;

pool=new ThreadPoolExecutor(DEFAULT_THREAD_COUNT,MAX_THREAD_COUNT,20,TimeUnit.SECONDS, new ArrayBlockingQueue(1000),new CallerBlocksPolicy());
        pool.prestartCoreThread();


// now repeatedly use

WorkerThread thread=new WorkerThread(pdfUri,cs);
 pool.execute(thread); // will submit new thread into the que.

// to shutdown

pool.awaitTermination(15,TimeUnit.SECONDS);
pool.shutdown();

No comments: