Concurrent programming in Java actual 1,2 chapter

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1 to promote the cause of processes and threads appear, is to address the following question:

2 process: operating system for resource allocation, process a variety of independent execution includes memory, file handles and safety certificate. Process through the communication mechanism between some coarse grain to exchange data, including: socket, a signal processor, shared within the           memory, semaphores and documents.

The 3 thread: thread allows multiple program control flow exist at the same time in the same process. Threads sharing process wide resources, such as memory and handle file handles, but each thread has its own program counter, stack and local variables. The thread is also known as       lightweight processes, in most modern operating system, is a thread is the basic scheduling unit.


4 threads have the advantage of

The risk brought by 5 threads

6 state variables and multiple threads access a variable leads to three kinds of errors

The 7 thread safety

    When multiple threads to access a class, no matter what the dispatching operation environment or the thread will be how to alternate execution, and in the main code does not need any additional synchronization or coordination, this class can exhibit the correct behavior, then call this is the line of Cheng Anquan.   the correct meaning, behavior of a class with standardized and consistent. In a good specification usually define various invariance condition to constrain the state of the object, and the definition of a posteriori conditional to describe object manipulation.

The 8 race conditions

   When the correctness depends a computation on multiple threads of execution timing alternation, it will happen.

  “Implementation of &rdquo after the first check; is a kind of race condition, it is the essence of observations a failure to make judgments based on or perform a calculation. The following code in LazyInitRace contains a race condition, it may undermine the validity of this class.

public class LazyInitRace {
     private ExpensiveObject instance = null;

     public ExpensiveObject getInstance() {
         if (instance == null) {
              instance = new ExpensiveObject();
         }   
         return instance;  
    }  
        
}    

9 built-in lock

   Java provides a built-in mechanisms to support atomic: synchronous code block. It consists of two parts: a reference as a lock object, as a protected by the lock code block. Each Java object can be used as a synchronization lock, the lock is called the built-in lock or monitor lock. Thread will automatically acquire the lock before entering a synchronized block, and automatically release the lock the exits the synchronization code blocks, and both exit through the control path of normal, or by throwing an exception out from the code block. The only way to get the built-in lock is to enter the lock protection synchronization code block or method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Rebecca at November 16, 2013 - 7:58 PM