Effective Java
Item 80: Prefer executors, tasks, and
streams to threads
Executor Framework - Basic Example
● Submit a runnable for execution
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(runnable);
exec.shutdown();
Executor Framework - invokeAny - Wait until first done
Executor Framework - invokeAny - Wait until ALL done
Executor Framework - invokeAny - Wait until ALL done
● Hard to get future result by complete sequence
Executor Framework - ExecutorCompletionService
● Get future result by sequence
Executor Framework - Cancel rest jobs
Executor Framework - Schedule task
Executor Framework - Choose executor
● Executors.newCachedThreadPool
○ Easy, no configuration
○ Do the right thing
○ When no available thread, new one will be added to pool
○ No task will be queued
○ Choose for small program
■ Why not heavy load program?
Executor Framework - Choose executor
● Executors.newCachedThreadPool
○ Easy, no configuration
○ Do the right thing
○ When no available thread, new one will be added to pool
○ No task will be queued
○ Choose for small program
■ Why not heavy load program?
● all of CPUs are fully utilized
● more tasks arrive, more threads will be created
● less CPU available, cause matters worse
Executor Framework - Choose executor
● Executors.newFixedThreadPool
○ Gives a pool with fixed number of threads
○ For heavy load program
○ Still need monitor queue size
Runnable & Callable
● Runnable: no return value
● Callable: return value
ForkJoinPool
● Normal Executor Thread Management
○ Main thread put task to queue
○ Sub tasks put tasks to queue
○ Every task in a queue without boundry
Main
Time
ForkJoinPool
● ForkJoinPool Thread Management
○ Main thread create tasks
○ Tasks create sub tasks to fork
○ tasks wait for sub tasks to join
○ Main thread get result or wait for done
Time
task1
Main
task1-1
task1-2
task2
task2-1
task2-2
Time
ForkJoinPool Example

Effective java item 80 prefer executors, tasks, and streams to threads

  • 1.
    Effective Java Item 80:Prefer executors, tasks, and streams to threads
  • 2.
    Executor Framework -Basic Example ● Submit a runnable for execution ExecutorService exec = Executors.newSingleThreadExecutor(); exec.execute(runnable); exec.shutdown();
  • 3.
    Executor Framework -invokeAny - Wait until first done
  • 4.
    Executor Framework -invokeAny - Wait until ALL done
  • 5.
    Executor Framework -invokeAny - Wait until ALL done ● Hard to get future result by complete sequence
  • 6.
    Executor Framework -ExecutorCompletionService ● Get future result by sequence
  • 7.
    Executor Framework -Cancel rest jobs
  • 8.
    Executor Framework -Schedule task
  • 9.
    Executor Framework -Choose executor ● Executors.newCachedThreadPool ○ Easy, no configuration ○ Do the right thing ○ When no available thread, new one will be added to pool ○ No task will be queued ○ Choose for small program ■ Why not heavy load program?
  • 10.
    Executor Framework -Choose executor ● Executors.newCachedThreadPool ○ Easy, no configuration ○ Do the right thing ○ When no available thread, new one will be added to pool ○ No task will be queued ○ Choose for small program ■ Why not heavy load program? ● all of CPUs are fully utilized ● more tasks arrive, more threads will be created ● less CPU available, cause matters worse
  • 11.
    Executor Framework -Choose executor ● Executors.newFixedThreadPool ○ Gives a pool with fixed number of threads ○ For heavy load program ○ Still need monitor queue size
  • 12.
    Runnable & Callable ●Runnable: no return value ● Callable: return value
  • 13.
    ForkJoinPool ● Normal ExecutorThread Management ○ Main thread put task to queue ○ Sub tasks put tasks to queue ○ Every task in a queue without boundry Main Time
  • 14.
    ForkJoinPool ● ForkJoinPool ThreadManagement ○ Main thread create tasks ○ Tasks create sub tasks to fork ○ tasks wait for sub tasks to join ○ Main thread get result or wait for done Time task1 Main task1-1 task1-2 task2 task2-1 task2-2 Time
  • 15.