Volatile, Atomic, synchronized performance and synchronization test program

Source: Internet
Author: User

Import java. util. concurrent. atomic. atomicInteger; public class Counter {public static int count = 0; public static volatile int volatileCount = 0; public static AtomicInteger atomicCount = new AtomicInteger (0 ); public static byte [] lock = new byte [0]; public static int syCount = 0; public static void sleep (final int gap) {try {Thread. sleep (gap);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public static void incrInt (final int gap) {count ++; sleep (gap);} public static void incrVolatileInt (int gap) {volatileCount ++; // It is related to the previous state of volatileCount and does not have atomicity sleep (gap);} public static void incrAtomicInt (final int gap) {// Method 1: // int tmp = atomicCount. get (); // atomicCount. getAndSet (tmp + 1); // Note: tmp + 1 is not atomic // Method 2: atomicCount. getAndIncrement (); // atomic sleep (gap);} public static void incrSynInt (final int gap) {synchronized (lock) {syCount ++; sleep (gap) ;}} public static void testInt (int count, final int gap) {long st = System. currentTimeMillis (); Thread threads [] = new Thread [count]; for (int I = 0; I <count; I ++) {threads [I] = new Thread (new Runnable () {@ Override public void run () {Counter. incrInt (gap) ;}}); threads [I]. start () ;}for (int I = 0; I <count; I ++) {try {threads [I]. join ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} long et = System. currentTimeMillis (); System. out. println ("--- running time =" + (et-st); // The value of each running operation may be different, which may be 1000System. out. println ("Run result: Counter. count = "+ Counter. count);} public static void testVolatileInt (int count, final int gap) {long st = System. currentTimeMillis (); Thread threads [] = new Thread [count]; for (int I = 0; I <count; I ++) {threads [I] = new Thread (new Runnable () {@ Override public void run () {Counter. incrVolatileInt (gap) ;}}); threads [I]. start () ;}for (int I = 0; I <count; I ++) {try {threads [I]. join ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} long et = System. currentTimeMillis (); System. out. println ("--- running time =" + (et-st); System. out. println ("Run result: Counter. volatileCount = "+ Counter. volatileCount);} public static void testAtomicInt (int count, final int gap) {long st = System. currentTimeMillis (); Thread threads [] = new Thread [count]; for (int I = 0; I <count; I ++) {threads [I] = new Thread (new Runnable () {@ Override public void run () {Counter. incrAtomicInt (gap) ;}}); threads [I]. start () ;}for (int I = 0; I <count; I ++) {try {threads [I]. join ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} long et = System. currentTimeMillis (); System. out. println ("--- running time =" + (et-st); System. out. println ("Run result: Counter. atomicCount = "+ Counter. atomicCount. get ();} public static void testSynInt (int count, final int gap) {long st = System. currentTimeMillis (); Thread threads [] = new Thread [count]; for (int I = 0; I <count; I ++) {threads [I] = new Thread (new Runnable () {@ Override public void run () {Counter. incrSynInt (gap) ;}}); threads [I]. start () ;}for (int I = 0; I <count; I ++) {try {threads [I]. join ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} long et = System. currentTimeMillis (); System. out. println ("--- running time =" + (et-st); System. out. println ("Run result: Counter. syCount = "+ Counter. syCount);} public static void main (String [] args) {int cnt = 100000; int gap = 2; testInt (cnt, gap); System. out. println ("-----------------------------------------"); testVolatileInt (cnt, gap); System. out. println ("-----------------------------------------"); testAtomicInt (cnt, gap); System. out. println ("-------------------------------------------"); testSynInt (cnt, gap );}}

 

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.