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 );}}