Locks in Java, whether lock or synchronized, can be divided into mutexes and non-mutex locks.
A mutex can only be held by one thread, and other threads can only wait for the lock to be released. The writelock of Synchronized,reentrantlock,readwritereentrantlock is mutually exclusive, but Readlock is not mutually exclusive.
The Filelock can be set to a mutex or a non-exclusive lock.
The implementation of the lock can be based on the operating system scheduling, but also in the form of spin to achieve.
Use the instructions of the operating system, let the thread wait, when the lock is available, let the thread wake up. This fit needs to wait for a long time. If the waiting time is short, the cost of this operation is greater.
With the loop constantly polling the state of the lock, the lock is available when it exits. This is the spin lock. In this way, basically do not do what the cycle is very CPU-intensive, if the time to wait for the lock is very long, in this manner is inappropriate.
The spin lock is implemented by the JVM, and the following example can simply describe the spin lock
Public classmywaitnotify3{monitorobject Mymonitorobject=NewMonitorobject (); Booleanwassignalled =false; Public voiddowait () {synchronized(mymonitorobject) { while(!wassignalled) { Try{
} Catch(interruptedexception e) {...} } //clear signal and continue running....
wassignalled =false; } } Public voiddonotify () {synchronized(mymonitorobject) {wassignalled=true; Mymonitorobject.notify (); } }}
Before other threads call Donotify,dowait will always spin, waiting for wassignalled to become true.
Disadvantages of spin Lock:
1. Spin lock always occupies the CPU, he has been running spin without a lock, so occupy the CPU, if not in a short period of time to obtain the lock, which will undoubtedly reduce the CPU efficiency.
2. It is possible to create a deadlock when using a spin lock, which can cause a deadlock when called recursively.
You can use the-xx:+usespinning to turn on the spin lock and use the-xx:preblockspin to set the number of spin waits.
There are times when we use locks that are completely unnecessary and can use escape analysis and lock elimination to improve the performance of the system.
For example, the following local variable, StringBuffer, is completely non-locking, but can affect performance.
Public string createnewstring (string a,string b) { new stringbuffer (); return Sb.append (a). Append (b);
Escape analysis and lock removal can be used with-xx:+doescapeanalysis and-xx:+eliminatelocks. Lock elimination requires the JVM to work in server mode.
Reentrant lock, which means that the inner recursive function still has the code to acquire the lock after the same thread's outer function acquires the lock, but it is not affected. Both Reentrantlock and synchronized are reentrant locks.
Public classTestImplementsrunnable{ Public synchronized voidget () {System.out.println (Thread.CurrentThread (). GetId ()); Set (); } Public synchronized voidset () {System.out.println (Thread.CurrentThread (). GetId ()); } @Override Public voidrun () {get (); } Public Static voidMain (string[] args) {Test ss=NewTest (); NewThread (ss). Start (); NewThread (ss). Start (); NewThread (ss). Start (); }}
Java Lock Concept Basics