代码改变世界

Java Concurrency In Practice - Chapter 1 Introduction

2014-05-14 23:15  小郝(Kaibo Hao)  阅读(237)  评论(0编辑  收藏  举报

1.1. A (Very) Brief History of Concurrency

motivating factors for multiple programs to execute simultaneously:

Resource utilization. Programs sometimes have to wait for external operations such as input or output, and while waiting can do no useful work. It is more efficient to use that wait time to let another program run.

   

Fairness. Multiple users and programs may have equal claims on the machine's resources. It is preferable to let them

share the computer via finer‐grained time slicing than to let one program run to completion and then start another.

   

Convenience. It is often easier or more desirable to write several programs that each perform a single task and have

them coordinate with each other as necessary than to write a single program that performs all the tasks.

   

Threads allow multiple streams of program control flow to coexist within a process.

   

Each thread has its own program counter, stack, and local variables.

   

1.2. Benefits of Threads

  1. Reduce development and maintenance costs.
  2. Improve the performance of complex applications.
  3. Make it easier to model how humans work and interact, by turning asynchronous workflows into mostly sequential ones.
  4. In GUI applications for improving the responsiveness of the user interface.
  5. In server applications for improving resource utilization and throughput.

   

1.2.1. Exploiting Multiple Processors

As it gets harder to scale up clock rates, processor manufacturers will instead put more processor cores on a single chip.

   

1.2.2. Simplicity of Modeling

When you have only one type of task to do, you can start at the top of the pile and keep working until the pile is exhausted (or you are); you don't have to spend any mental energy figuring out what to work on next. On the other hand, managing multiple priorities and deadlines and switching from task to task usually carries some overhead.

   

1.2.3.Simplified Handling of Asynchronous Events

A server application that accepts socket connections from multiple remote clients may be easier to develop when each connection is allocated its own thread and allowed to use synchronous I/O.

   

1.2.4. More Responsive User Interfaces

The long running task is instead executed in a separate thread, the event thread remains free to process UI events, making the UI more responsive.

   

1.3 Risks of Threads

1.3.1 Safety Hazards

1.3.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 can occur in sequential programs is an inadvertent infinite loop, where the code that follows the loop never gets executed.

1.3.3. Performance Hazards

Performance issues subsume poor service time, responsiveness, throughput, resource consumption, or scalability.

   

  Runtime overhead

  1. Context switches when the scheduler suspends the active thread temporarily so another thread can run.
  2. 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.

       

1.4 Threads are Everywhere

JVM housekeeping tasks (garbage collection, finalization)

   

Frameworks introduce concurrency into applications by calling application components from framework threads. Components invariably access application state, thus requiring that all code paths accessing that state be thread-safe.

   

Timer, Servlet and JSPs, Remote Method Invocation(RMI) - RMI lets you invoke methods on objects running in another JVM.

A remote object must guard against two thread safety hazards: properly coordinating access to state that may be shared with other objects, and properly coordinating access to the state of the remote object itself (since the same object may be called in multiple threads simultaneously).