2011年4月16日星期六

Java Concurrency In Practice读书笔记之Introduction

Benefits of Threads
1.Exploiting Multiple Processors
2.Simplicity of Modeling
  A complicated, asynchronous workflow can be decomposed into a number of simpler, synchronous workflows each running in a separate thread, interacting only with each other at specific synchronization points.
3.Simplified Handling of Asynchronous Events
  If an application goes to read from a socket when no data is available, read blocks until some data is available.If each request has its own thread, then blocking does not affect the processing of other requests.
4.More Responsive User Interfaces

Risks of Threads
1.Safety Hazards
@NotThreadSafe
public class UnsafeSequence {
    private int value;

    /** Returns a unique value. */
    public int getNext() {
        return value++;
    }
}

2.Liveness Hazards
  A liveness failure occurs when an activity gets into a state such that it is permanently unable to make forward progress. One form of liveness failure that canoccur in sequential programs is an inadvertent infinite loop, where the code that follows the loop never gets executed.Including deadlock, starvation, and livelock.

3.Performance Hazards
  Performance issues subsume a broad range of problems, including poor service time, responsiveness, throughput, resource consumption, or scalability.Context switches are more frequent in applications with many threads, and have significant costs: saving and restoring execution context, loss of locality, and CPU time spent scheduling threads instead of running them. When threads share data, they must use synchronization mechanisms that can inhibit compiler optimizations, flush or invalidate memory caches, and create synchronization traffic on the shared memory bus. All these factors introduce additional performance costs.


没有评论:

发表评论