public
Last active

An example of using ExecutorService framework in Java for developing concurrent applications

  • Download Gist
ExecutorServiceSample.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
 
public class ExecutorServiceSample {
 
public static void main(String[] args) {
 
try {
List<List<Integer>> integerArrays = loadDataFromFile();
List<Integer> sumOfEachArray = calculateSequentially(integerArrays);
System.out.println("Sequential Sum");
printSumArray(sumOfEachArray);
ConcurrentHashMap<String, Integer> sumMap = calculateUsingThreads(integerArrays);
System.out.println("Multi-Threaded sum");
printSumMap(sumMap);
 
sumMap = calculateUsingExecutor(integerArrays);
System.out.println("Executor Sum");
printSumMap(sumMap);
 
sumOfEachArray = calculateSumUsingCallable(integerArrays);
System.out.println("Callable Sum");
printSumArray(sumOfEachArray);
 
} catch (IOException e) {
e.printStackTrace();
}
}
 
private static List<Integer> calculateSequentially(List<List<Integer>> integerArrays) {
List<Integer> sumOfEachArray = new ArrayList<>();
for ( List<Integer> integers : integerArrays){
int sum = 0;
for(Integer i : integers){
sum+=i;
}
sumOfEachArray.add(sum);
}
return sumOfEachArray;
}
private static ConcurrentHashMap<String, Integer> calculateUsingThreads(
List<List<Integer>> integerArrays){
 
ConcurrentHashMap<String, Integer> sumMap =
new ConcurrentHashMap<>(integerArrays.size());
List<Thread> threads = new ArrayList<>();
int i = 1;
//Create thread instances for each array
for ( List<Integer> integers : integerArrays){
String arrayName = "Array "+i;
Thread thread = new Thread(
new ArraySumCalculator(sumMap,integers,arrayName));
threads.add(thread);
i++;
}
 
//Now launch all the threads at the same time
for ( Thread thread : threads){
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
 
return sumMap;
}
private static ConcurrentHashMap<String, Integer> calculateUsingExecutor(
List<List<Integer>> integerArrays){
 
ConcurrentHashMap<String, Integer> sumMap = new
ConcurrentHashMap<>(integerArrays.size());
 
//Number of threads = 1 more than number of processors.
ExecutorService executor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors()+1);
int i = 1;
for(List<Integer> integers : integerArrays){
String arrayName = "Array "+i;
executor.execute(
new ArraySumCalculator(sumMap,integers, arrayName));
i++;
}
executor.shutdown();
 
try {
executor.awaitTermination(60,TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
 
return sumMap;
}
public static List<Integer> calculateSumUsingCallable(
List<List<Integer>> integerArrays){
 
List<Callable<Integer>> callables = new ArrayList<>(integerArrays.size());
 
List<Integer> arraySum = new ArrayList<>(integerArrays.size());
//Create callables for each array
for ( List<Integer> integers : integerArrays){
Callable<Integer> callable = new ArraySumCallable(integers);
callables.add(callable);
}
 
//get fixed thread pool executor
ExecutorService executorService = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors() + 1);
 
 
try {
 
//Submit all the callables and obtain their Futures
List<Future<Integer>> futures =
executorService.invokeAll(callables);
 
executorService.shutdown();
 
//Iterate through the futures and get the sum
for ( Future<Integer> future : futures){
Integer sum = future.get();
 
arraySum.add(sum);
 
 
}
 
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
System.out.println("Exception while calculating sum");
}
 
return arraySum;
 
}
 
private static List<List<Integer>> loadDataFromFile()
throws IOException {
Path path = Paths.get(PATH_TO_ARRAYS_CONTENTS);
List<String> linesInFile = Files.readAllLines(path, Charset.defaultCharset());
List<List<Integer>> arrays = new ArrayList<>();
for ( String s : linesInFile){
String [] sArray = s.split(",");
List<Integer> integers = new ArrayList<>();
for ( String sInt : sArray){
integers.add(Integer.parseInt(sInt));
}
arrays.add(integers);
}
 
return arrays;
}
private static void printSumArray(List<Integer> sumOfEachArray){
int i = 1;
for ( Integer sum : sumOfEachArray){
System.out.println("Array "+i+":"+sum);
i++;
}
}
private static void printSumMap(ConcurrentHashMap<String, Integer> sumMap){
List<String> mapKeys = Collections.list(sumMap.keys());
Collections.sort(mapKeys, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String[] o1Array = o1.split(" ");
String[] o2Array = o2.split(" ");
Integer o1Int = Integer.parseInt(o1Array[1]);
Integer o2Int = Integer.parseInt(o2Array[1]);
return o1Int.compareTo(o2Int);
}
});
for ( String s : mapKeys){
System.out.println(s+":" + sumMap.get(s));
}
}
}
 
class ArraySumCalculator implements Runnable{
 
ConcurrentHashMap<String, Integer> sumMap;
List<Integer> integers;
String arrayName;
ArraySumCalculator(ConcurrentHashMap<String, Integer> sumMap,
List<Integer> integers,
String arrayName){
this.sumMap = sumMap;
this.integers = integers;
this.arrayName = arrayName;
}
@Override
public void run() {
int sum = 0;
for ( Integer i : integers){
sum += i;
}
sumMap.put(arrayName, sum);
}
}
 
class ArraySumCallable implements Callable<Integer>{
 
List<Integer> integers;
 
ArraySumCallable(List<Integer> integers){
 
this.integers = integers;
}
 
@Override
public Integer call() throws Exception {
 
Integer sum = 0;
 
for (Integer i : integers){
sum += i;
}
 
return sum;
 
}
}
// The contents of the PATH_TO_ARRAYS_CONTENTS file:
//4,5,6,7,84,567,1234,678,23,1234,567,89,22,32,56,77,63
//67,12,34,21,23,81,49,4,1,9,49,2312
//123,78,234,568,98,38,234,9,7,4,2,1,23,67
//34,56,78,96,23,58,28,18,235,875,12,6,7,8,4
//75,2435,7,345,234,123,1267,4,56,73,4,5,6,7,8
//23,78,234,568,98,38,234,9,7,4,2,1,2,67,61,8
//534,56,78,96,23,58,28,18,23,875,12,6,7,8,4,12,7
//5,255,7,345,24,123,17,4,56,73,4,5,6,7,8
//13,78,234,568,98,38,234,9,7,4,2,1,23,67
//4,56,78,96,23,58,28,18,235,875,12,6,7,8,4
//7,235,7,35,234,123,126,4,56,73,4,5,6,7,8

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.