Java Callable and Runnable memo

jerry80409
3 min readSep 1, 2021

-

Callable

A callable instance can return a result.

Callable<Object> callable = new Callable() {
@Override
public Object call() throws Exception {
return doSomething();
}
}

Lambda expressions

// doSomething return an Object
Callable<Object> callable = () -> doSomething();

Runnable

A runnable instance just executes and has no return.

Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("do something no return");
}
};

Lambda expressions

// doSomething but no return
Runnable
runnable = () -> System.out.println(
"do something no return");

Run with threads-pool

Testing

Setting a stateless method.

private String statelessMethod(String target) {
String upperCase = target.toUpperCase();
System.out.println(Thread.currentThread().getName() + " task executed: " + upperCase);
return upperCase;
}

Callable

@Test
void callableTasks() {
ExecutorService threadPool = Executors.newFixedThreadPool(10);
System.out.println("mainThread: " + Thread.currentThread().getName()); // a(97) ~ z(122) callable tasks to uppercase
List
<Callable<String>> callableTasks = IntStream
.range(97, 123)
.mapToObj(i -> String.valueOf((char) i))
.map(s -> (Callable<String>) () -> statelessMethod(s))
.collect(Collectors.toList());
callableTasks.forEach(threadPool::submit);
}

Result

mainThread: Test worker
pool-1-thread-4 task executed: D
pool-1-thread-5 task executed: E
pool-1-thread-9 task executed: I
pool-1-thread-4 task executed: K
pool-1-thread-4 task executed: N
pool-1-thread-7 task executed: G
pool-1-thread-5 task executed: L
pool-1-thread-1 task executed: A
pool-1-thread-9 task executed: M
pool-1-thread-8 task executed: H
pool-1-thread-5 task executed: Q
pool-1-thread-7 task executed: P
pool-1-thread-9 task executed: S
pool-1-thread-4 task executed: O
pool-1-thread-3 task executed: C
pool-1-thread-10 task executed: J
pool-1-thread-1 task executed: R
pool-1-thread-3 task executed: Y
pool-1-thread-6 task executed: F
pool-1-thread-4 task executed: X
pool-1-thread-10 task executed: Z
pool-1-thread-7 task executed: V
pool-1-thread-9 task executed: W
pool-1-thread-8 task executed: T
pool-1-thread-2 task executed: B
pool-1-thread-5 task executed: U

Runnable

@Test
void runnableTasks() {
ExecutorService threadPool = Executors.newFixedThreadPool(10);
System.out.println("mainThread: " + Thread.currentThread().getName()); // a(97) ~ z(122) callable tasks to uppercase
final List
<Runnable> runnableTasks = IntStream
.range(97, 123)
.mapToObj(i -> String.valueOf((char) i))
.map(s -> (Runnable) () -> statelessMethod(s))
.collect(Collectors.toList());
runnableTasks.forEach(threadPool::submit);
}

Result

mainThread: Test worker
pool-1-thread-1 task executed: A
pool-1-thread-8 task executed: H
pool-1-thread-7 task executed: G
pool-1-thread-10 task executed: J
pool-1-thread-1 task executed: K
pool-1-thread-4 task executed: D
pool-1-thread-8 task executed: M
pool-1-thread-5 task executed: E
pool-1-thread-7 task executed: L
pool-1-thread-4 task executed: P
pool-1-thread-4 task executed: T
pool-1-thread-4 task executed: U
pool-1-thread-4 task executed: V
pool-1-thread-4 task executed: W
pool-1-thread-4 task executed: X
pool-1-thread-4 task executed: Y
pool-1-thread-4 task executed: Z
pool-1-thread-9 task executed: I
pool-1-thread-1 task executed: O
pool-1-thread-10 task executed: N
pool-1-thread-6 task executed: F
pool-1-thread-5 task executed: R
pool-1-thread-2 task executed: B
pool-1-thread-3 task executed: C
pool-1-thread-8 task executed: Q
pool-1-thread-7 task executed: S

--

--

jerry80409

隨便記錄一些沒有整理很清楚的想法