s4553711
7/26/2017 - 3:19 AM

ExecServiceTest.java

package org.ibms.bioit.runner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by s4553711 on 2017/7/19.
 */
public class ExecServiceTest {
    public static class WordLengthCallable implements Callable {
        private int word;
        public WordLengthCallable(int upper) {
            this.word = upper;
        }
        public Integer call() {
            int result = 0;
            for(int i = 1; i < word; i++) {
                result += i;
            }
            return Integer.valueOf(result);
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        int nThreads = Integer.valueOf(args[0]);
        ExecutorService pool = Executors.newFixedThreadPool(nThreads);
        int jobs = Integer.valueOf(args[1]);
        int jobSize = Integer.valueOf(args[2]);

        List<Future<Integer>> result= new ArrayList<>();
        for(int i = 1; i <= jobs; i++) {
            Callable<Integer> callable = new WordLengthCallable(jobSize);
            Future<Integer> future = pool.submit(callable);
            result.add(future);
        }

        System.out.println("Before");
        int sum = 0;
        for (Future<Integer> future : result) {
            sum += future.get();
        }
        System.out.println(sum);
        System.exit(0);
    }
}