Java synchronization method and synchronization code block, Object lock, Class lock Difference

Source: Internet
Author: User

/** * @author Admin * @date 2018/1/12 9:48 * Function on the same instance object to discuss * synchronized Synchronous method Test * Two threads, one thread calls synchronized adornment method, another thread can call non s Ynchronized modified method, non-impact */public class Synchronizedtest {public synchronized void MethodA () {try {fo                R (int i = 0; i < 5; i++) {System.out.println ("methoda-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace (); }} public void MethodB () {try {for (int i = 0; i < 5; i++) {System.out.print                ln ("methodb-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace ();        }} public static void Main (string[] args) {synchronizedtest test = new Synchronizedtest (); Thread thread1 = new Thread (new Runnable () {@Override public void run () {Test.metho            DA ();        }        }); THread1.start (); Thread thread2 = new Thread (new Runnable () {@Override public void run () {Test.metho            DB ();        }        });    Thread2.start (); }} Run Result: methoda-0methodb-0methoda-1methodb-1methodb-2methoda-2methoda-3methodb-3methoda-4methodb-4

 

/** * @author Admin * @date 2018/1/12 10:16 * Function on the same instance object to discuss * sychronized code block Test * Two threads, one thread executes synchronized code block, another thread executes non-synch                Ronized code block */public class SychronizedTest2 {public void MethodA () {synchronized (this) {try {                    for (int i = 0; i < 5; i++) {System.out.println ("methoda-" + i);                Thread.Sleep (1000);            }} catch (Interruptedexception e) {e.printstacktrace (); }}} public void MethodB () {try {for (int i = 0; i < 5; i++) {System.                Out.println ("methodb-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace ();        }} public static void Main (string[] args) {SychronizedTest2 test2 = new SychronizedTest2 (); Thread thread1 = new Thread (new Runnable () {@Override public void run () {tesT2.methoda ();        }        });        Thread1.start (); Thread thread2 = new Thread (new Runnable () {@Override public void run () {Test2.meth            OdB ();        }        });    Thread2.start (); }} Run Result: methoda-0methodb-0methoda-1methodb-1methoda-2methodb-2methodb-3methoda-3methoda-4methodb-4

 

/** * @author Admin * @date 2018/1/12 10:33 * Function on the same instance object discussion * Synchronized synchronous method and Synchronous code block * 1, synchronized, and synchronized (this) both    No difference, all functions on the This object lock, so will synchronize * 2, synchronized (obj), this is on the Obj object lock, and this object lock is different, so will not synchronize */public class SynchronizedTest3 { Public synchronized void MethodA () {try {for (int i = 0; i < 5; i++) {SYSTEM.OUT.PR                Intln ("methoda-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace (); }} public void MethodB () {synchronized (this) {try {for (int i = 0; i < 5; I                    + +) {System.out.println ("methodb-" + i);                Thread.Sleep (1000);            }} catch (Interruptedexception e) {e.printstacktrace ();        }}} public void Methodc () {Object obj = new Object (); Synchronized (obj) {try {for (int i = 0; i< 5;                    i++) {System.out.println ("methodc-" + i);                Thread.Sleep (1000);            }} catch (Interruptedexception e) {e.printstacktrace ();        }}} public static void Main (string[] args) {SynchronizedTest3 test3 = new SynchronizedTest3 (); Thread thread1 = new Thread (new Runnable () {@Override public void run () {test3.            MethodA ();        }        });        Thread1.start (); Thread thread2 = new Thread (new Runnable () {@Override public void run () {Test3.meth            OdB ();        }        });        Thread2.start (); Thread thread3 = new Thread (new Runnable () {@Override public void run () {Test3.meth            OdC ();        }        });    Thread3.start (); }} Run Result: Methoda-0methodc-0methoda-1methodc-1methoda-2methodc-2methoda-3methodc-3methoda-4methodc-4methodb-0meThodb-1methodb-2methodb-3methodb-4 

 

/** * @author Admin * @date 2018/1/12 10:48 * function is discussed on the same class, each class has only one class lock * Synchronized class lock * static synchronized and synchronized (  Synchronizedtest4.class), all functions are locked in the same class, so sync */public class SynchronizedTest4 {public synchronized static void MethodA ()                {try {for (int i = 0; i < 5; i++) {System.out.println ("methoda-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace ();  }} public void MethodB () {synchronized (synchronizedtest4.class) {a try {for (int i = 0; I < 5;                    i++) {System.out.println ("methodb-" + i);                Thread.Sleep (1000);            }} catch (Interruptedexception e) {e.printstacktrace ();        }}} public static void Main (string[] args) {SynchronizedTest4 test4 = new SynchronizedTest4 (); Thread thread1 = new Thread (new Runnable() {@Override public void run () {Test4.methoda ();        }        });        Thread1.start (); Thread thread2 = new Thread (new Runnable () {@Override public void run () {Test4.meth            OdB ();        }        });    Thread2.start (); }} Run Result: methoda-0methoda-1methoda-2methoda-3methoda-4methodb-0methodb-1methodb-2methodb-3methodb-4

 

/** * @author Admin * @date 2018/1/12 11:03 * Synchronized object lock and static synchronized class lock, is two different locks, so will not sync * Two threads, one call object lock, one call class lock */public class SynchronizedTest5 {public synchronized void MethodA () {try {for (int i = 0; I &lt ; 5;                i++) {System.out.println ("methoda-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace ();                }} public synchronized static void MethodB () {try {for (int i = 0; i < 5; i++) {                System.out.println ("methodb-" + i);            Thread.Sleep (1000);        }} catch (Interruptedexception e) {e.printstacktrace ();        }} public static void Main (string[] args) {SynchronizedTest5 test5 = new SynchronizedTest5 (); Thread thread1 = new Thread (new Runnable () {@Override public void run () {Test5.meth            OdA ();     }        });   Thread1.start (); Thread thread2 = new Thread (new Runnable () {@Override public void run () {Test5.meth            OdB ();        }        });    Thread2.start (); }} Run Result: methoda-0methodb-0methoda-1methodb-1methodb-2methoda-2methodb-3methoda-3methodb-4methoda-4

Java synchronization method and synchronization code block, Object lock, Class lock Difference

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.