There is no doubt that reentrantlock is far better than synchronize in multi-thread scenarios.
Recently, I want to write a program. There is a variable that is read and write by a thread in most cases, and in a few cases, multiple threads read and write concurrently. Therefore, we need to test the efficiency comparison between reentrantlock and synchronize in a single thread.
An interesting phenomenon was found during the test.
For the test code, see the following section.
Test code 1 result:
Nolocktime: 0:00:00. 004
Nolocktime: 0:00:00. 006
Nolocktime: 0:00:00. 000
Synctime: 0:00:02. 902
Synctime: 0:00:02. 902
Synctime: 0:00:02. 886
Reentranttime: 0:00:05. 012
Reentranttime: 0:00:05. 007
Reentranttime: 0:00:04. 270
It was quite strange to see the test results. The reentrantlock was so much slower than synchronize.
But then I carefully studied the test results and found that the JVM optimized the test code! Observe the nolocktime, which is almost 0!
Add the Statistical Code of total to get the following test results:
Add the test code result of total statistics:
Nolocktime: 0:00:03. 786
Total: 1078760960
Nolocktime: 0:00:03. 774
Total: 1078760960
Nolocktime: 0:00:03. 428
Total: 1078760960
Synctime: 0:00:05. 553
Total: 1078760960
Synctime: 0:00:05. 555
Total: 1078760960
Synctime: 0:00:04. 826
Total: 1078760960
Reentranttime: 0:00:05. 451
Total: 1078760960
Reentranttime: 0:00:05. 424
Total: 1078760960
Reentranttime: 0:00:04. 493
Total: 1078760960
This is a normal result.
Summary:
- I have heard that the JVM can optimize the code at runtime. It is indeed a pretty cool function, and useless code is optimized directly at runtime.
- The test program should pay attention to the prevention of JVM and other optimizations.
- Reentrantlock and synchronize have the same efficiency in a single thread (from the implementation principle, in the case of a single thread, they are all a comparison command)
- In a single thread, reentrantlock and synchronize are very efficient, and each call takes about 10-11 seconds.
Test code 1:
import java.util.concurrent.locks.ReentrantLock;import org.apache.commons.lang3.time.StopWatch;public class XXX {static ReentrantLock lock = new ReentrantLock(false);static int size = 100;static int count = 100000000;static StopWatch watch = new StopWatch();public static void main(String[] args) {noLockTime();noLockTime();noLockTime();syncTime();syncTime();syncTime();ReentrantTime();ReentrantTime();ReentrantTime();}static void noLockTime(){//int total = 0;watch.reset();watch.start();for(int i = 0; i < count; ++i){testNotLock();//total += testNotLock();}watch.stop();System.out.println("noLockTime: " + watch);//System.out.println("total:" + total);}static void syncTime(){//int total = 0;watch.reset();watch.start();for(int i = 0; i < count; ++i){testSync();//total += testSync();}watch.stop();System.out.println("syncTime: " + watch);//System.out.println("total:" + total);}static void ReentrantTime(){//int total = 0;watch.reset();watch.start();for(int i = 0; i < count; ++i){testReentrantLock();//total += testReentrantLock();}watch.stop();System.out.println("ReentrantTime: " + watch);//System.out.println("total:" + total);}static int testNotLock(){int sum = 0;for(int i = 0; i < size; ++i){sum += i;}return sum;}static synchronized int testSync(){int sum = 0;for(int i = 0; i < size; ++i){sum += i;}return sum;}static int testReentrantLock(){try{lock.lock();int sum = 0;for(int i = 0; i < size; ++i){sum += i;}return sum;}finally{lock.unlock();}}}
Add the test code for total statistics:
import java.util.concurrent.locks.ReentrantLock;import org.apache.commons.lang3.time.StopWatch;public class XXX {static ReentrantLock lock = new ReentrantLock(false);static int size = 100;static int count = 100000000;static StopWatch watch = new StopWatch();public static void main(String[] args) {noLockTime();noLockTime();noLockTime();syncTime();syncTime();syncTime();ReentrantTime();ReentrantTime();ReentrantTime();}static void noLockTime(){int total = 0;watch.reset();watch.start();for(int i = 0; i < count; ++i){//testNotLock();total += testNotLock();}watch.stop();System.out.println("noLockTime: " + watch);System.out.println("total:" + total);}static void syncTime(){int total = 0;watch.reset();watch.start();for(int i = 0; i < count; ++i){//testSync();total += testSync();}watch.stop();System.out.println("syncTime: " + watch);System.out.println("total:" + total);}static void ReentrantTime(){int total = 0;watch.reset();watch.start();for(int i = 0; i < count; ++i){//testReentrantLock();total += testReentrantLock();}watch.stop();System.out.println("ReentrantTime: " + watch);System.out.println("total:" + total);}static int testNotLock(){int sum = 0;for(int i = 0; i < size; ++i){sum += i;}return sum;}static synchronized int testSync(){int sum = 0;for(int i = 0; i < size; ++i){sum += i;}return sum;}static int testReentrantLock(){try{lock.lock();int sum = 0;for(int i = 0; i < size; ++i){sum += i;}return sum;}finally{lock.unlock();}}}