From: http://blog.csdn.net/liaomin416100569/archive/2010/01/11/5172652.aspx
The synchronized modifier indicates that the same object is displayed as a synchronization queue in different threads.
If different objects are instantiated, synchronized will not have the synchronization effect.
1. Object lock
All objects automatically contain a single lock.
JVM is responsible for tracking the number of times an object is locked. If an object is unlocked, its count is 0. When the task (thread) locks the object for the first time, the Count changes to 1. Every time this same task (thread) gets a lock on this object, the Count increases progressively.
Only the task (thread) that first obtains the lock can continue to obtain multiple locks on the object.
Every time a task leaves a synchronized method, the Count decreases. When the count is 0, the lock is completely released, and other tasks can use this resource.
2. Synchronized synchronization Block
2.1 synchronize to a single object lock
When a synchronization block is used, if all the synchronization blocks under the method are synchronized to the lock on an object, all tasks (threads) can only be mutually exclusive.
Resource1.java demonstrates three threads (including the main thread) trying to enter the synchronization blocks of three different methods of a class, although these synchronization blocks are in different methods, but because it is synchronized to the same object (the current object synchronized (this), their methods are still mutually exclusive.
For example
Class Test
{
Public static user = NULL;
Public synchronized void add (User U)
{
User = u;
Dao. Save (User)
}
}
If test = new test ();
User u = new user ();
U. setusername ("liaomin ");
U. setuserpassword ("liaomin ");
Test. Add (U );
If test tes1t = new test ();
User U1 = new user ();
U1.setusername ("huqun ");
U1.setuserpassword ("huqun ");
Tes1t. Add (U1 );
Now thread 1 and thread 2 start at the same time if the object new is not the same test
If there is a thread crossover, the data inserted into the database will be the same.
Because when your user variable is static, you assign a value to it for the first time, if there is no save
When another thread changes the user value, the first thread inserts the value, that is, the value assigned for the second time.
So to achieve synchronization, you can change the method to static to achieve the synchronization effect.
Modify as follows:
Public static synchronized void add (User U)
{
User = u;
Dao. Save (User)
}
The method to change to static exists in the heap.
The global method only exists for all instantiated and uninstantiated objects, so a synchronization queue will appear.
Of course, you can use lock without static.
Class Test
{
Public static user = NULL;
Lock = new reentrantlock ();
Public void add (User U)
{
Lock. Lock ();
User = u;
Dao. Save (User );
Lock. Unlock ();
}
}
In this way, no matter how many new objects you have, they will be synchronized by threads.
Equivalent
Public static synchronized void add (User U)
{
User = u;
Dao. Save (User)
}
At the same time, the lock performance is higher than synchronized.
You only need to manually disable lock.
PS:
The main point is that lock can complete all functions implemented by synchronized.
The main difference is that lock has more precise thread semantics and better performance than synchronized. Synchronized Automatically releases the lock, which must be manually released by the programmer and must be released in the finally clause.