Source Code : LinkedBlockingQueue and ThreadPoolExecutor
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
LinkedBlockingQueue and ThreadPoolExecutor
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
for (int i = 0; i < 10; i++) {
final int localI = i;
queue.add(new Runnable() {
public void run() {
doExpensiveOperation(localI);
}
});
}
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1000,
TimeUnit.MILLISECONDS, queue);
executor.prestartAllCoreThreads();
executor.shutdown();
executor.awaitTermination(100000, TimeUnit.SECONDS);
}
private static void doExpensiveOperation(int index) {
System.out.println("Starting expensive operation " + index);
try {
Thread.sleep(index * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ending expensive operation " + index);
}
}
Thank with us