Original article address:
Http://blog.csdn.net/huang_xw/article/details/7318554
/*** @ Description: two threads (thread 1 and thread 2) access the internal synchronization method syn1 () and synchronization method syn2 () of the same object. * result: when thread 1 accesses the synchronization method syn1 () of the object sameobj, thread 2 accesses the synchronization method syn2 () in the object sameobj. or thread 1 is blocked when thread 2 is accessed. * conclusion: when different threads access the same synchronization method of the same object, the threads are mutually exclusive. * In fact, when different threads access different Synchronization Methods of the same object, the threads are also mutually exclusive. * Changing the synchronization method to the synchronization block is also true. the key factor is that an object is locked. * @ author Snoopy * @ blog http://blog.csdn.net/huang_xw */package basic. B _syn; import Org. apache. log4j. logger; public class testsync {private Static logger = logger. getlogger (testsync. class); // thread 1 static class T1 implements runnable {testsync s; Public T1 (testsync sameobj) {This. S = sameobj;} // thread 1 access synchronization method 1 Public void run () {logger. debug ("Starting thread 1 ........... "); S. syn1 (); logger. debug ("Exit thread 1 ........... ") ;}} // thread 2 static class T2 implements runnable {testsync s; Public T2 (testsync sameobj) {This. S = sameobj;} // thread 2 Access synchronization method 2 Public void run (){ Logger. debug ("start thread 2 ........... "); S. syn2 (); logger. debug ("Exit thread 2 ........... ") ;}} // Object synchronization method 1 Public synchronized void syn1 () {logger. debug ("▲▲▲▲▲▲▲access synchronization method 1 from thread 1"); try {logger. debug ("thread 1 is accessing synchronization method 1 !!! "); Thread. sleep (3000);} catch (interruptedexception e) {e. printstacktrace ();} logger. debug ("▲▲▲▲▲▲▲thread 1 exit synchronization method 1");} // Object synchronization method 2 Public synchronized void syn2 () {logger. debug ("●●●●●● thread 2 start to access synchronization method 2"); For (INT I = 0; I <5; I ++) {try {logger. debug ("thread 2 is accessing synchronization method 2 !!! "); Thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace () ;}} logger. debug ("●●●●●●● thread 2 exit synchronization method 2");} public static void main (string [] ARGs) {// This is the same object to be accessed by multiple threads. testsync sameobj = new testsync (); // thread 1 accesses synchronization method 1 and thread 2 also accesses synchronization method 2. thread T1 = new thread (New T1 (sameobj); thread t2 = new thread (New T2 (sameobj); t1.start (); t2.start ();}}
Execution result:
0 [thread-0] Debug basic. B _syn.testsync-start thread 1 ........... 1 [thread-0] Debug basic. B _syn.testsync-▲▲▲▲▲▲method 11 [thread-0] Debug basic when thread 1 begins to access synchronization. B _syn.testsync-thread 1 is accessing synchronization method 1 !!! 1 [thread-1] Debug basic. B _syn.testsync-start thread 2 ........... 3001 [thread-0] Debug basic. B _syn.testsync-▲▲▲▲▲▲method for exiting synchronization from thread 1 13001 [thread-0] Debug basic. B _syn.testsync-exit thread 1 ........... 3001 [thread-1] Debug basic. B _syn.testsync-●●●●●●● access the synchronization method from thread 2 23001 [thread-1] Debug basic. B _syn.testsync-thread 2 is accessing synchronization method 2 !!! 4001 [thread-1] Debug basic. B _syn.testsync-thread 2 is accessing synchronization method 2 !!! 5001 [thread-1] Debug basic. B _syn.testsync-thread 2 is accessing synchronization method 2 !!! 6002 [thread-1] Debug basic. B _syn.testsync-thread 2 is accessing synchronization method 2 !!! 7002 [thread-1] Debug basic. B _syn.testsync-thread 2 is accessing synchronization method 2 !!! 8006 [thread-1] Debug basic. B _syn.testsync-●●●●●●● thread 2 exit synchronization method 28007 [thread-1] Debug basic. B _syn.testsync-exit thread 2 ...........