Synchronized syntax: 1, synchronized statement; 2, Synchronized method
1,Monitorenter, and monitorexit byte codes, depending on the mutex lock of the underlying operating system .
2, will be translated into a common method call and return instructions such as: invokevirtual, Areturn instructions
Principle: User thread blocking, kernel thread booting, design to user line to switch to kernel thread, cost is large
JVM Optimizations for Locks
One, biased lock (biased Locking)
jdk1.6 is turned on by default. Parameter open mode:-xx:+usebiasedlocking, start the default five seconds after the effective. can take effect immediately:-xx:biasedlockingstartupdelay=0
Note: This lock is a JVM internal lock, not an app lock. So if optimization has an impact on the entire application, you need to be cautious.
In a competitive situation without too much optimization effect, it is possible to reduce performance.
Principle: 1, the lock object has the MARKWORD flag bit, the current thread, the lock state
| current thread | Timestamp of the LOCK | age | Lock marker bit | whether locked
Benefits: In the competitive environment, and the same thread multiple requests, can be fully implemented with synchionrized, code introduction, maintenance convenience. In fact, there is less synchronization
For example, more than 70% of requests may not be designed to lock the competition
Cons: In a highly competitive environment, this lock has no high performance for other locks developed at all
Instance:
public class Baised {
Internal use of Sync Lock
public static list<integer> numberlist=new vector<integer> ();
public static void Main (string[] args) {
Long Begin=system.currenttimemillis ();
int count=0;
int startnum=0;
while (count<10000000) {
Numberlist.add (Startnum);
startnum+=2;
count++;
}
Long End=system.currenttimemillis ();
System.out.println ("time-->" + (End-begin));
}
}
JVM Parameters:-xx:+usebiasedlocking-xx:biasedlockingstartupdelay=0-xmx512m-xms512m
time required: time-->345
JVM Parameters:-xx:-usebiasedlocking-xmx512m-xms512m
time required: time-->542
Second, lightweight lock (lightweight Locking)
When a thread enters a lightweight lock, it assigns a value markword to its own line stacks. Use CAs to replace assignments, add locks, and if failures enter an expansion lock.
Three, lock expansion
Entering the expansion lock, the system mutex is called into the weight lock, which may hang. Kernel thread switching, with a large cost
Iv. Adaptive Optional Lock (Adaptive Spinning)
After entering the optional, in the critical area to win a certain number of times. The JVM optimizes itself after the spin count of 1.7.
V. Lock removal (locking elimination)
At JIT compile time, the lock that cannot compete with the shared resource is removed through context scanning. So to improve performance.
Example:
public class Lockeliminate {
private static final int CIRCLE = 2000000;
public static void Main (String args[]) {
Long Begin=system.currenttimemillis ();
for (int i=0;i<circle;i++) {
Createstringbuffer ("JVM", "lockeliminate");
}
Long End=system.currenttimemillis ();
System.out.println ("time-->" + (End-begin));
}
public static string Createstringbuffer (string s1,string s2) {
StringBuffer sb=new StringBuffer ();
Sb.append (S1);
Sb.append (S2);
return sb.tostring ();
}
}
JVM Parameters:-server-xx:+doescapeanalysis-xx:+eliminatelocks
time-->127
JVM parameters:-server-xx:+doescapeanalysis-xx:-eliminatelocks
time-->269
For Markword Reference
[Java cultivation Path 10]-JVM synchronized principle or optimization