how to prove that ther is queue in the Thread pool which assigns one thread for each task , and when there are more number tasks than thread , then the task will be waited in the queue , and whenever a free thread is available in the queue , thread pool assigns a task from the queue to the free thread
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 {
// Here i am makeing the currenlty executing thread to sleep for some time so that thread has the monitor lock with it, and but at this time so many tasks waiting for the lock so eventually those will waited on the queue.
Thread.sleep(900);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class FixedThreadPoolExecutor {
/**
* @param args
*/
public static void main(String[] args) {
/*
* this will create 10 threads in the pool ,
* which are waiting for the tasks to execute
*/
ExecutorService e = Executors.newFixedThreadPool(3);
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());
}
}
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 {
// Here i am makeing the currenlty executing thread to sleep for some time so that thread has the monitor lock with it, and but at this time so many tasks waiting for the lock so eventually those will waited on the queue.
Thread.sleep(900);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class FixedThreadPoolExecutor {
/**
* @param args
*/
public static void main(String[] args) {
/*
* this will create 10 threads in the pool ,
* which are waiting for the tasks to execute
*/
ExecutorService e = Executors.newFixedThreadPool(3);
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