I have used a similar program as below to achieve multithreading to run parallel process but before completing few process my thread is moving to another (or) it is not completing the process completely, I am write some 5 files in parallel with data per thread where out of 5 sometimes 4 files are only writing. Please see the same code I refer,
private static final Random PRNG = new Random();
private static class Result {
private final int wait;
public Result(int code) {
this.wait = code;
}
}
public static Result compute(Object obj) throws InterruptedException {
int wait = PRNG.nextInt(3000);
Thread.sleep(wait);
return new Result(wait);
}
public static void main(String[] args) throws InterruptedException, ExecutionException
{
List<Object> objects = new ArrayList<Object>();
for (int i = 0; i < 1000; i++) {
objects.add(new Object());
}
List<Callable<Result>> tasks = new ArrayList<Callable<Result>>();
for (final Object object : objects) {
Callable<Result> c = new Callable<Result>() {
@Override
public Result call() throws Exception {
return compute(object);
}
};
tasks.add(c);
}
ExecutorService exec = Executors.newCachedThreadPool();
// some other exectuors you could try to see the different behaviours
// ExecutorService exec = Executors.newFixedThreadPool(3);
// ExecutorService exec = Executors.newSingleThreadExecutor();
try {
long start = System.currentTimeMillis();
List<Future<Result>> results = exec.invokeAll(tasks);
int sum = 0;
for (Future<Result> fr : results) {
sum += fr.get().wait;
System.out.println(String.format("Task waited %d ms",
fr.get().wait));
}
long elapsed = System.currentTimeMillis() - start;
System.out.println(String.format("Elapsed time: %d ms", elapsed));
System.out.println(String.format("... but compute tasks waited for total of %d ms; speed-up of %.2fx", sum, sum / (elapsed * 1d)));
} finally {
exec.shutdown();
}
}
May I know any better solution we can do for multi-threading to achieve once the process completes the thread should exit out from the process and I am using Java8,
Updated process code,
public String compute(String obj) throws InterruptedException {
MyProcess myProc=new MyProcess(writeFiles(obj));
myProc.generateReport();
}
public void processMethod() {
List<Callable<String>> tasks = new ArrayList<Callable<String>>();
for (final String object : list) {
Callable<String> c = new Callable<String>() {
@Override
public String call() throws Exception {
return compute(object);
}
};
tasks.add(c);
}
ExecutorService exec = Executors.newCachedThreadPool();
try {
long start = System.currentTimeMillis();
List<Future<String>> results = exec.invokeAll(tasks);
String sum=null;
}
finally {
exec.shutdown();
}
try {
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
}
}
Consider the writeFiles will read and write data from database to local file which is huge in memory and need to compare the 5 files which contains difference, where in this case for one time all the files are getting written and for others only one file is getting written and total time of thread is getting shared to all the pool-threads and within the time duration it is not possible to write all the files.