Java
Concurrent layer
Write parallel operations with ExecutorService.
Tasks with ExecutorService
In this lesson you will run a task concurrently with ExecutorService. Executors.newSingleThreadExecutor() creates a single-thread pool; submit takes a Callable and returns a Future.
ExecutorService pool = Executors.newSingleThreadExecutor();
Future<Integer> future = pool.submit(task);
int total = future.get();
pool.shutdown();
future.get() blocks until the task completes and returns the result. In a single-thread pool the output order is deterministic, so the expected output is the same every time. When done, close the pool with shutdown.
Callable and Future
Callable<V> is a task that returns a result; its call() method returns a value of type V. Future<V> holds a result that will appear later; .get() blocks until it is ready.
Callable<Integer> task = () -> {
int sum = 0;
for (Transaction t : ledger) sum += t.amount;
return sum;
};
This task sums all amounts in the list. You run it with submit(task) and get the result with future.get(). The lambda implements Callable's single method automatically.
Exercise Compute the concurrent total
Use the previous lesson's ledger list. Create the pool with ExecutorService pool = Executors.newSingleThreadExecutor();. Write a Callable<Integer> task lambda that sums all amounts in the list. Get the result with pool.submit(task).get() and print it with System.out.println. Finally call pool.shutdown().
Show example solution
import java.util.ArrayList;
import java.util.concurrent.*;
class Transaction {
String category;
int amount;
Transaction(String category, int amount) {
this.category = category;
this.amount = amount;
}
}
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<Transaction> ledger = new ArrayList<>();
ledger.add(new Transaction("market", 120));
ledger.add(new Transaction("fuel", 80));
var filtered = ledger.stream()
.filter(t -> t.amount > 100)
.toList();
ExecutorService pool = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> {
int sum = 0;
for (Transaction t : ledger) sum += t.amount;
return sum;
};
Future<Integer> future = pool.submit(task);
int total = future.get();
pool.shutdown();
System.out.println(total);
}
}
Test yourself
-
What does ExecutorService replace?
- A class definition
- A stream operation
- Hand-creating a Thread
- Writing tests
Show answer
Hand-creating a Thread — ExecutorService replaces direct new Thread usage.
-
Complete the submit call
ExecutorService pool = Executors.newSingleThreadExecutor();
Future<Integer> future = pool.____(task);
Show answer
ExecutorService pool = Executors.newSingleThreadExecutor();
Future<Integer> future = pool.submit(task);
-
Match the concurrent concepts
- ExecutorService
- Task pool
- Callable
- Result-bearing task
- Future
- Pending result
- submit
- Sends the task
Next lesson: Test layer