Monday, June 30, 2014

Executor

package Algorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorImplementation {
@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(20);
//case1
for (int i = 0; i <= 3; i++) {
a.execute(new a());
// List<Runnable> b = a.shutdownNow();
// System.out.println(b);
}
//pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1 in
// shutdownnow attempt to shut all running task and return list of task that never executed
// in shutdown case no new task will be accepted in our case pool size was 20.3 is already started from pool and now in case2 for i=0 to 5 no execution will be done with exception thrown as
//ask Algorithm.a@68fede rejected from java.util.concurrent.ThreadPoolExecutor@134b58c[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 4]
// case2
a.shutdown();
for (int i = 0; i <= 5; i++) {
// so before executing this task we can check i it shudown with method isShutdown
a.execute(new a());
}
}

public void Test1() {
ExecutorService a = Executors.newFixedThreadPool(5);
for (int i = 0; i <= 5; i++) {
a.execute(new a());
}

for (int i = 0; i <= 5; i++) {
a.execute(new a());
}
}

public void Test2() throws InterruptedException, ExecutionException {
ExecutorService a = Executors.newFixedThreadPool(5);
List<Future> resultet=new ArrayList<Future>();
for (int i = 0; i <= 5; i++) {
Future reult=a.submit(new b());
resultet.add(reult);
}
for(Future individualresult:resultet){
individualresult.get();
}

for (int i = 0; i <= 5; i++) {
a.execute(new a());
}
for (int i = 0; i <= 5; i++) {
Future s = a.submit(new a());
s.get();
}
}

public void Test3() throws InterruptedException, ExecutionException {
ExecutorService a = Executors.newFixedThreadPool(5);
List<Pojo> pojolist = new ArrayList<Pojo>();
for (int i = 0; i <= 5; i++) {
pojolist.add(new Pojo());
}
List<ExecutorImplementation.c> list = new ArrayList<ExecutorImplementation.c>();
for (int i = 0; i <= pojolist.size(); i++) {
ExecutorImplementation.c ind = new ExecutorImplementation.c(
pojolist.get(i));
list.add(ind);

}
List<Future<Pojo>> result = a.invokeAll(list);
// iterate all the list for getting the return value ;
result.get(0).get();

}


public void Test4() throws InterruptedException, ExecutionException {
ExecutorService a = Executors.newFixedThreadPool(5);
List<Pojo> pojolist = new ArrayList<Pojo>();
for (int i = 0; i < 5; i++) {
pojolist.add(new Pojo());
}
List<d> list = new ArrayList<d>();
for (int i = 0; i < pojolist.size(); i++) {
d ind = new d(pojolist.get(i));
list.add(ind);

}
List<Future<Pojo>> result = a.invokeAll(list);

}

class c implements Callable<Pojo> {
private Pojo pojo;

private int i = 0;

c(Pojo pojo) {
this.pojo = pojo;

}

@Override
public Pojo call() throws Exception {
return new Pojo();
}
}
}

class a implements Runnable {
private int i = 0;

@Override
public void run() {
i = 1;
i = i + 1;
System.out.println("This is testig ");
System.out.println(i);
}

}

class b implements Callable<Pojo> {

@Override
public Pojo call() throws Exception {
return new Pojo();
}
}

class d implements Callable<Pojo> {
private Pojo pojo;

private int i = 1;

d(Pojo pojo) {
this.pojo = pojo;

}

@Override
public Pojo call() throws Exception {
i++;
System.out.println(pojo.getId());
System.out.println(pojo.getName());
System.out.println(i);
return pojo;
}
}

class Pojo {
private String name = "ram";

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id
*            the id to set
*/
public void setId(int id) {
this.id = id;
}

private int id = 11;

}

No comments:

Post a Comment