Test the efficiency of Java synchronize and reentrantlock in a single thread

Source: Internet
Author: User

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:

  1. 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.
  2. The test program should pay attention to the prevention of JVM and other optimizations.
  3. 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)
  4. 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();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.