Java Concurrency


Task (runnable or callable)

Synchoronus Processing:  called in the main thread. Main Thread is blocked till the task processing is finished.

Asynchoronus Processing: main thread delegates to an independe thread via Service/Executor.

Executor (Thread Pool ex: Fixed or Scheduled ) & Service (Lifecycle methods for executor):

– core size, max. number of threads

– work queue

– rejection policy

– thread factory

Each Java thread has 2 stacks ( one native & one java)

resouce thrashing: application is not able to perform due to lack of resources.

How do you size a thread pool?

– benefits are to use many resources like multiple processors & exploit I/O.

– upper bound for a compute intensive task is N or N+1 (num of processors).

– for I/O based activities it is N * target_utilization_rate*(1+(server_time/wait_time))

– Alternate to TP is thread per task and single thread per task type (like in AWT)

– notify() causes fewer context switches than notifyAll(), hard but good to use notify() where u can because of better performance characteristics.

Handling Blocked methods:

  1. Blocking Queue: timedpolling as opposed to take which blocks indefinitely. Though easy to implement, responsiveness is poor.
  2. Convert the task to future by using submit and call cancel on Future. Better responsiveness.
  3. Post a poision pill
  4. Interrupt the thread waiting on blocking method! One potential problem is the thread is already stuck  on a blocking call. So, invoking another method like shutdown() {which calls interrupt} is also executed on the same thread that is waiting for the blocked method to finish!

Another reason not to interrupt the thread is, it is borrowed from a service and it is not good to make assumption about the borrowed thread’s cancellation policy. or even what task that thread may be running. One exception is for a thread running in standard executor, you can call the “boolean cancel(boolean mayInterrupIfRunning)” with true for the future returned through Executor.submin()