5. Runnable (vs) Callable
• Runnable (vs) Callable comes into point when we are using Executer
framework.
• ExecutorService is a subinterface of Executor, which accepts both
Runnable and Callable tasks.
• Earlier Multi-Threading can be achieved using Interface
Runnable(Since 1.0), but here the problem is after completing the
thread task we are unable to collect the Threads information. In-order
to collect the data we may use Static fields.
7. Visualize The Callable And Future Example
thread-pool
Task 2
Task 1 Task 3 …
Blocking queue
t0 t1 t2 t3 …. t10
Fetch next task
From queue
Execute it
main thread
Future<Integer> future = service.submit(new Task());
placeholder
Once result is ready
3 Set value of placeholder
8. Visualize The Callable And Future Example
thread-pool
Task 2
Task 1 Task 3 …
Blocking queue
t0 t1 t2 t3 …. t10
Fetch next task
From queue
Execute it
main thread
Future<Integer> future = service.submit(new Task());
placeholder
Once result is ready
3 Set value of placeholder
future.get()
blcoked
runnable
Run dependent taks
9. There Is A Problem
thread-pool
Task 2
Task 1 Task 3 …
Blocking queue
t0 t1 t2 t3 …. t10
Fetch next task
From queue
Execute it
main thread
for 1..4
service.submit(new Task())
Once result is ready
future.get()
blcoked
runnable
Run dependent taks
7 5
1 7 5
11. Independent Flows
Fetch Order
Enrich Order
payment
Dispatch
Send Email
Fetch Order
Enrich Order
payment
Dispatch
Send Email
Fetch Order
Enrich Order
payment
Dispatch
Send Email
Independent Flow
……………….