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:

Best Client Optimized Programming Language for Mobile Development?

Use Google's Dart language, which is Object-oriented, class-based and garbage-collected language with C-Style.  Dart can compile to eith...