Saturday, May 19, 2012

Executor Framework TIP # 3

how to make a thread pool will create the threads as and when a task is coming and no free thread is available
(please read the Executor Framework TIP # 2 , to understand more )

try to execute this code after understanding the TIP # 2


package com.yagapp.phaseone;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


class Task implements Runnable{
public void run(){
System.out.println("ramesh");

try {
Thread.sleep(900);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

public class FixedThreadPoolExecutor {

/**
* @param args
*/


public static void main(String[] args) {


ExecutorService e = Executors.newCachedThreadPool();

e.execute(new Task());
e.execute(new Task());
e.execute(new Task());
e.execute(new Task());
e.execute(new Task());
e.execute(new Task());
e.execute(new Task());



}

}

No comments:

Post a Comment