Monday, June 30, 2014

shutdownNow

Program1
package Algorithm;

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

public class shutdownNow {
@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(10);
for (int i = 0; i <= 20; i++) {
a.execute(new shutdownnowclass());
}
// The main point  of both shut down and shut down now is that no new task will be executed after calling these method .An exception will be thrown if tried   to execute after calling these methods
List<Runnable> listofthatneverexecuted=a.shutdownNow();
System.out.println(listofthatneverexecuted);

}

}

class shutdownnowclass implements Runnable {

@Override
public void run() {
for (int i = 0; i <= 2; i++) {
System.out.println("in");

}

}
}

Program2
package Algorithm;

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

public class shutdown {

@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(20);
for (int i = 0; i <= 3; i++) {
a.execute(new shutdownclass());
}
// When shut down is called the execution is shut down .Below code is not executed .An exception is thrown .To cure exception following checking can be done
//@if(!a.isShutdown()){
// }
for (int i = 0; i <= 5; i++) {
a.execute(new shutdownclass());
}
a.shutdown();
// now there is no exception as following line is never executed
if(!a.isShutdown()){
for (int i = 0; i <= 5; i++) {
a.execute(new shutdownclass());
}
}
}

}

class shutdownclass implements Runnable {

@Override
public void run() {
for (int i = 0; i <= 1000000000; i++) {
System.out.println("in");

}

}

}




No comments:

Post a Comment