Search This Blog

Total Pageviews

Multi Thread Example using Execution Service

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static java.util.Arrays.asList ;

public class ExecutorServiceMultiThreadSubstraction
    static class Substract implements Callable<Long> {
        private long b;
        private long a;
        Substract(long a, long b){
            this.a = a ;
            this.b = b ;
        public Long call() throws Exception
            return a-b;
        public static void main(String str[]) throws Exception{
            ExecutorService executor = Executors.newFixedThreadPool(5) ;
            List<Future<Long>> results = executor.invokeAll(asList(new Substract(0,10), new Substract(11,13), new Substract(14,15) )) ;            
            executor.shutdown() ;            
            for(Future<Long> result: results)
                System.out.println(result.get()) ;

No comments:

You are currently using plaintext http to connect.

Npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation must use TLS 1.2 or higher. S...