Skip to content

Instantly share code, notes, and snippets.

@lelodois
Last active April 19, 2024 02:25
Show Gist options
  • Save lelodois/491efd378c44e08318deee07f7adef74 to your computer and use it in GitHub Desktop.
Save lelodois/491efd378c44e08318deee07f7adef74 to your computer and use it in GitHub Desktop.
Using Executor Service
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
public class MyTask {
private Callable<Boolean> createTask(Integer amount, MyTaskParam taskParam) {
return () -> taskParam.isAnyGreaterTo(amount);
}
public static void main(String[] args) throws InterruptedException {
Collection<Callable<Boolean>> tasks = new ArrayList<>();
for (int i = 0; i < ; i++) {
Callable<Boolean> task = new MyTask().createTask(10, new MyTaskParam(i, new ArrayList<>()));
tasks.add(task);
}
Executors.newFixedThreadPool(20).invokeAll(tasks);
}
}
class MyTaskParam {
private Integer amount;
private List<MyTaskParam> children;
MyTaskParam(Integer amount, List<MyTaskParam> children) {
this.amount = amount;
this.children = children;
}
public Boolean isAnyGreaterTo(Integer otherAmount) {
if (amount > otherAmount)
for (MyTaskParam child : children)
return child.isAnyGreaterTo(otherAmount);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment