As we all know, in Java multithreaded programming, a very important aspect is the thread synchronization problem.
For synchronization of threads, there are generally the following solutions:
1. Add the Synchronized keyword to the method signature of the method that needs to be synchronized.
2. Use the synchronized block to synchronize the code snippets that need to be synchronized.
3. Use the Lock object in the Java.util.concurrent.lock package provided in JDK 5.
In addition, in order to solve the security problems that may occur when multiple threads are accessing the same variable, we can not only adopt the synchronization mechanism, but also ensure better concurrency through the threadlocal added in JDK 1.2.
In this article, we will discuss the Java Multithreading Synchronization mechanism in detail, and make a discussion on threadlocal.
The approximate directory structure is as follows:
One, the arrival of the thread--question: Why should there be multiple thread synchronization? What is the mechanism of Java multithreaded synchronization?
Two, give me a lock, I can create a rule--what is the traditional multithreaded synchronous programming method? What are their similarities and differences?
Three, lock came, we all move aside--java in the framework of the lock detailed.
Iv. collaboration, mutual exclusion, collaboration--java multithreaded collaboration (wait, notify, Notifyall)
Five, you have I have all have--threadlocal how do I resolve concurrent security?
Vi. Summary of several methods of--java thread safety comparison.
One, the arrival of the thread
Let's cite an example of a dirty: a restaurant has a small bathroom and can only accommodate one person to the toilet. In order to ensure uninterrupted, the toilet people into the bathroom, will lock the door. We can imagine the bathroom as a shared resource, and many people who need to go to the toilet can be seen as multiple threads. If the toilet is currently occupied, then other people have to wait until the person has gone to the toilet, open the door to come out. This is like when multiple threads share a resource, they must be arrival.
Someone said, "What if I don't have this door?" Let two threads compete with each other, who gets the first, who can work, so much better? But we know that if there is no door to the toilet, and the people who go to the toilet together, then there will be a dispute, the normal steps of the toilet will be disrupted, it is possible to have unexpected results, such as some people may have to be forced to fertilize in the wrong place ...
It is because of this door that anyone who goes to the toilet alone can successfully complete their toilet process without being interfered with, or even result from. This means that when you go to the toilet, pay attention to arrival.
So how can you guarantee that when multiple threads compete for the same resource in a Java multithreaded programming, they won't have a "fight"? It is said to use the synchronization mechanism. Yes, like the example above, a typical synchronization case, once the first one starts to go to the toilet, the second must wait for the first to end before he can start the toilet. A thread that, once in a process, must wait for a normal return and exit the process before the next thread can start the process. Here, the most important thing is the bathroom door. In fact, the bathroom door as a resource lock role, as long as the toilet lock the door, it is equivalent to obtain the lock, and when he opened the lock out, it is equivalent to release the lock.
In other words, multithreaded thread synchronization mechanism is actually controlled by the concept of lock. So how does a lock manifest in a Java program?