I. Thread Synchronization
Thread Synchronization usually refers to the various threads that do similar things, and cannot access the same shared variable at the same time.
Process Synchronization Methods:
1. SynchronizationCodeBlock
Synchronized (object ){
Code block to be synchronized
}
If you put all the code in the run method into the synchronization block, it will become a single thread and will lose its meaning.
Advantages and disadvantages of Synchronous Code blocks:
Benefits: multi-thread security issues.
Disadvantage: Each thread only needs to obtain the CPU execution right and judge the lock each time, which consumes more resources. More secure, more troublesome! Advantages and disadvantages!
2. synchronous Functions
The synchronization function does not explicitly specify the synchronization lock. How does the synchronization function implement synchronization? If a function needs to be called by an object, the function has a reference to the object -- this, so the function is synchronized.
The lock used is this.
Now we know that the synchronization lock of a common function is this, so the static method is not called by an object. Without this, what is the synchronization lock used? Its lock is the Class byte corresponding to the class
Code file object.
After trial, the two threads respectively execute the synchronization code block and static method with ticket. class as the lock above. These two threads have successfully implemented process synchronization! This indicates that the static method is indeed a bytecode file.
The object is a synchronization lock.
The following issues need to be clarified during thread synchronization:
1. First, identify the code that is run in multiple threads.
2. Identify which data is shared data
3. Determine which statements in multi-threaded code are used to share data.
Thread Synchronization
Ii. Thread Communication
Thread Communication usually refers to the various threads that do different types of things. In addition to being unable to access Shared variables at the same time, they also need to communicate with each other under certain conditions. For example
Producer consumer. When the product pool is empty, the consumer is blocked. If the producer produces a product, the consumer is notified to consume the product;
When the product pool is full, the producer is blocked. If the consumer consumes a product, the producer is notified to be able to produce the product. Number of Products
In addition to being unable to perform the operation at the same time, we also need to pay attention to the situation where the number of products is 0 and N. This is the difference between thread communication and thread synchronization.
1. Multiple producers and consumers, using notity causes all waiting Problems
The wake-up thread does not wake up, resulting in a deadlock. For example, two producers a, B, and two consumers m, and N. When the number of products is 0, assume that at a certain time, the consumer M consumes a product. At this time, both a, B and n are waiting at the same time, and N is at the top of the CPU queue, when the consumer y is called, n threads are awakened, but the number of products is 0, then the consumer n is waiting. It is reasonable to wake up the other party, but the wake-up party causes all four threads to wait.
In this case, you can solve the problem by using policyall. Wake up all threads, so that all threads will not wait.
2. Stop the thread
There is only one way, that is, the run method ends. As long as the loop is controlled, the method can end, that is, the thread end. Iterrupte forcibly restores a frozen thread to the running state. That is, to clear the thread
Frozen status.
Iii. Lock
The lock class is the update solution for the newly added synchronization lock in jdk1.5. LockThe implementation providesSynchronizedMethods and statements can obtain a wider range of lock operations. This implementation allows a more flexible structure, can have very different attributes, can support multiple relatedConditionObject.
A lock is a tool that controls multiple threads to access shared resources. Generally, a lock provides exclusive access to shared resources. Only one thread can obtain the lock at a time. All access to shared resources must first obtain the lock. However, some locks may allow concurrent access to shared resources, suchReadwritelock.
SynchronizedThe use of methods or statements provides access to the implicit monitor lock related to each object, but forces the acquisition and release of all locks to appear in a block structure: when multiple locks are obtained, they must be released in reverse order, and all locks must be released within the same lexical range as when all locks are obtained.
AlthoughSynchronizedThe range mechanism of methods and statements makes it much easier to use the monitor lock programming. It also helps avoid many common programming errors involving locks, but sometimes it is necessary to use locks in a more flexible way. For exampleAlgorithmRequire "hand-over-Hand" or "chain locking": Get the Lock of node A, then get the lock of Node B, then release a and get C, release B and get d, and so on.LockThe implementation of the interface allows the lock to be acquired and released in different scopes, and allows multiple locks to be acquired and released in any order, thus supporting the use of this technology.
As flexibility increases, it also brings more responsibilities. No block structure lock is used.SynchronizedMethod and statement. In most cases, use the following statement:
Lock L =...; L. Lock (); try {// access the resource protected by this lock} finally {L. Unlock ();}