Synchronized is used for multi-threaded design, with the Synchronized keyword, the running results of multi-thread programming will become controllable. The Synchronized keyword is used to protect shared data.
Synchronized implement the mechanism of synchronization: synchronized relies on "lock" mechanism for multi-threaded synchronization, "lock" there are 2 kinds, One is Object lock, one is class lock .
1. Rely on object lock lock
When an object is initialized, it automatically has an object lock. synchronized {Normal method} relies on object lock work, multi-threaded access to the Synchronized method, once a process grabs the lock, the other processes only queued.
Synchronized {Normal method} relies on object lock work, multi-threaded access to the Synchronized method, once a process grabs the lock, the other processes only queued. synchronized void method{} function, equivalent to void method{synchronized (this) {...}} Through the code to see more clearly:
public class Testsynchronized {public synchronized void method1 () throws Interruptedexception {System.out.prin TLN ("Method1 begin at:" + system.currenttimemillis ()); Thread.Sleep (6000); System.out.println ("Method1 end at:" + system.currenttimemillis ()); } public synchronized void Method2 () throws Interruptedexception {while (true) {System.out.println (" METHOD2 running "); Thread.Sleep (200); }} static Testsychronized instance = new testsychronized (); public static void Main (string[] args) {Thread thread1 = new Thread (new Runnable () {@Override public void Run () {try {instance.method1 (); } catch (Interruptedexception e) {e.printstacktrace (); } for (int i=1; i<4; i++) {try {thread.sleep (200); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.println ("Thread1 still Alive"); } } }); Thread thread2 = new Thread (new Runnable () {@Override public void run () {try { INSTANCE.METHOD2 (); } catch (Interruptedexception e) {e.printstacktrace (); } } }); Thread1.start (); Thread2.start (); }}
Running result:thread2 wait until method1 in Thread1 executes METHOD2, stating Method1 and method2 mutex
Synchronized {modifier code block} is not only useful for this, synchronized void method{} whole function plus synchronized block, efficiency is not good. Inside the function, we may need to synchronize only a small portion of the shared data, other data that can be freely accessed, when we can
more precise control with synchronized (expression) {//statement}。
- 2.synchronized {static method} this code block is equivalent to
void method{synchronized (Obl.class)}} uses the lock of the class object of the class to do the shared mutex of the thread.
Package Com.free4lab.lol;public class Testsychronized {public synchronized static void Method1 () throws Interruptedexc eption {System.out.println ("METHOD1 begin at:" + system.currenttimemillis ()); Thread.Sleep (6000); System.out.println ("Method1 end at:" + system.currenttimemillis ()); } public synchronized static void Method2 () throws Interruptedexception {while (true) {SYSTEM.OUT.PR Intln ("Method2 running"); Thread.Sleep (200); }} static testsychronized Instance1 = new testsychronized (); static testsychronized Instance2 = new testsychronized (); public static void Main (string[] args) {Thread thread1 = new Thread (new Runnable () {@Override public void Run () {try {instance1.method1 (); } catch (Interruptedexception e) {e.printstacktrace (); } for (int i=1; i<4; i++) { try {thread.sleep (200); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.println ("Thread1 still Alive"); } } }); Thread thread2 = new Thread (new Runnable () {@Override public void run () {try { INSTANCE2.METHOD2 (); } catch (Interruptedexception e) {e.printstacktrace (); } } }); Thread1.start (); Thread2.start (); }}
The output effect is also method1 and method2 mutex
- 3.synchronized {The lock of the Run method}run method .
This example is more
Package Com.free4lab.lol;public class Testsychronized { static testsychronized instance = new testsychronized (); public static void Main (string[] args) { thread thread1 = new Thread (new Runnable () { @Override public Synchro nized void Run () {for (int i=1; i<4; i++) { try { thread.sleep () } catch (Interruptedexception e) { e.printstacktrace (); } System.out.println ("Thread1 still alive," + i);}} ); New Thread (Thread1). Start (); New Thread (Thread1). Start (); }}
If the synchronized current thread takes out all the data, the lock is released and the output is ordered
Thread1 still alive, 1thread1 still alive, 2thread1 still alive, 3thread1 still alive, 1thread1 still alive, 2thread1 stil L Alive, 3
Synchronized modifies common methods, modifies static methods, modifies code blocks, modifies thread run method comparisons