Using ExecutorService
I submit a batch of tasks and tasks have a time variable, namely GENERAL_TIME
share between them. I want to set the value of GENERAL_TIME
just before submitting tasks. Here is the code:
ExecutorService executor = Executors.newWorkStealingPool();
ArrayList<Callable<Boolean>> callables = new ArrayList<>();
long GENERAL_TIME = 0;
for (String i : host.getHosts()){
callables.add(
() -> {
ESBRun.monitorOSMetrics(
db, COMPONNENT_TYPE, GENERAL_TIME, metric, ssh, i
);
return true;
}
}
GENERAL_TIME = System.currentTimeMillis();
executor.invokeAll(callables)
.stream()
.map(future -> {
try {
return future.get();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
});
}
but it errors
variable used in lambda should be final or effectively final java
How can I solve that?