Chapter 6: executor
Pros and cons:
Item | Pro | Con |
---|---|---|
Single thread server | Easy and simple | Doesn't exploit concurrency |
A server that creates new thread for each request | Exploits concurrency | Slow: thread creating overhead; may hit system resource limit |
ThreadPoolExecutor | Fast; Won't hit the system limit | ? |
Thread vs Runnable vs Callable
Thread vs Runnable or Callable
Thread is a class, while the other two are interfaces.
Extend thread only if you want to override some behavior (http://stackoverflow.com/a/2426408/2682639). Implementing runnable and callable leaves you more flexibility. See abstract class vs interface in Effective Java.
Runnable vs Callable:
Runnable is a fairly limiting abstraction; run cannot return a value or throw checked exceptions, although it can have side effects such as writing to a log file or placing a result in a shared data structure.
Callable is a better abstraction: it expects that the main entry point, call, will return a value and anticipates that it might throw an exception.