Synchronized is the primary synchronization tag in Java
1 Synchronous non-static method
Scope range is only the current object synchronization between different threads, if n is a test object, between different test objects, is equal to no synchronization, the method can only synchronize n for the object in the test class
public class Testpublic synchronized Void Inc () {n++;}}
If you want to synchronize n outside the class, using a synchronous block object, the object is also an out-of-class object, and there is only one relative to all test objects, so you can synchronize
Final integer lock = new Integer (1), class Test implements Runnable {public int value = 0;public void Inc () {SYNCHR Onized (lock) {m++;value++;n++;}}
2 Synchronous static methods
public class testpublic static synchronized Void Inc () {n++;}}
The scope range is all test class objects, and the Inc method can only be called by one at a time between all test objects
3 Synchronizing code blocks
Publci class test{ public Void Inc () {synchronized (lock) {//m++;value++;n++;}}}
Scope range, depending on the scope range of lock relative to the class
If lock is an object other than this class, lock is a global variable relative to all test objects, and the synchronized synchronization range is between all objects
If lock is an object within that class, such as an in-class variable, lock is a local local variable relative to all test objects, and the synchronized synchronization range is between different threads of the same object, as in synchronous non-static methods
Test code
Import Java.util.concurrent.countdownlatch;import Java.util.concurrent.countdownlatch;public class TestVolatile { public static volatile int n = 0; Public integer m = 0;final integer lock = new Integer (1), class MyThread implements Runnable {public int value = 0;PUBL IC Void Inc () {synchronized (lock) {m++;value++;n++;}} public void Run () {{for (int i = 0; i < i++) try {//System.out.println (i); Inc ();//m++;//Vboolean = Vboolean = True? False:true; Thread.Sleep (3); To make the run result more random, delay 3 milliseconds} catch (Exception e) {}}}}static class MyThread1 implements Runnable {public static Integer LocalLock = new Integer (2);p ublic static synchronized Void Inc () {n++;} public void Run () {for (int i = 0; i <; i++) try {//inc (); Method One//Method two synchronized (locallock) {n = n + 1;} Thread.Sleep (3);} catch (Exception e) {}}}class MyThread2 implements Runnable {public void run () {synchronized (lock) {for (int i = 0; I < ; 100; i++) try {m++; Thread.Sleep (3); To make the run result more random, delay 3 milliseconds} catch (Exception e) {}}}} 3 and 2 code, just one is a separate lock, a single lock, the efficiency is about 10 times times the difference. Class MyThread3 implements Runnable {public void Inc () {synchronized (lock) {m++;}} public void Run () {for (int i = 0; i <; i++) try {Inc (); Thread.Sleep (3); To make the run result more random, delay 3 milliseconds} catch (Exception e) {}}}public value g_value = new Value ();p ublic class Value{public synchronized Voi D Inc () {m++;}} 4 and 3 function almost, just MYTHREAD4, using global variable G_value lock, synchronization between different threads class MyThread4 implements Runnable {public int value = 0;public void I NC () {{g_value.inc ();}} public void Run () {for (int i = 0; i <; i++) try {Inc (); Thread.Sleep (3); To make the run result more random, delay 3 milliseconds} catch (Exception e) {}}}public void Runthread () throws interruptedexception {Long beg = System.curren Ttimemillis (); mythread1[] mythreadary = new mythread1[100]; thread[] Threadary = new Thread[100];for (int i = 0; i < threadary.length; i++) {Mythreadary[i] = new MyThread1 (); Threa Dary[i] = new Thread (Mythreadary[i], "test1");} for (int i = 0; i < threadary.length; i++) {Threadary[i].start ();} for (int i = 0; i < threadary.length; i++) {threadary[i].join (); System.out.println ((Mythreadary[i]). value);} System.out.println (TESTVOLATILE.N); System.out.println (m); SYSTEM.OUT.PRINTLN ("The program Takes time:" + (System.currenttimemillis ()-beg));} public static void Main (string[] args) throws interruptedexception {testvolatile tmp = new Testvolatile (); Tmp.runthread () ;}}
A little understanding of the Java multi-line program