address: http://blog.csdn.net/huang_xw/article/details/7318554
/*** @ Description: two threads (thread 1 and thread 2) access the same object's internal synchronous method Syn () and non-synchronous method nonsyn () * result: when thread 1 accesses the synchronization method of the object sameobj, thread 2 can simultaneously access the non-synchronous method of the object sameobj. * @ author Snoopy * @ blog http://blog.csdn.net/huang_xw */package basic. B _syn; import Org. apache. log4j. logger; public class testsyna {Private Static logger = logger. getlogger (testsyna. class); // thread 1 static class T1 implements runnable {testsyna s; Public T1 (testsyna sameobj) {thi S. S = sameobj;} // thread 1 access the synchronization method public void run () {logger. debug ("Starting thread 1 ........... "); S. syn (); logger. debug ("Exit thread 1 ........... ") ;}} // thread 2 static class T2 implements runnable {testsyna s; Public T2 (testsyna sameobj) {This. S = sameobj;} // thread 2 access non-synchronous method public void run () {logger. debug ("start thread 2 ........... "); S. nonsyn (); logger. debug ("Exit thread 2 ........... ") ;}} // Object synchronization method public synchronized void Syn () {logger. debug ("▲▲▲▲▲▲thread 1 is enabled Access synchronization method "); try {thread. sleep (3000);} catch (interruptedexception e) {e. printstacktrace ();} logger. debug ("▲▲▲▲▲▲▲thread 1 exit the synchronization method");} // non-synchronous public void nonsyn () {logger. debug ("●●●●●● thread 2 to access the non-synchronous method"); For (INT I = 0; I <5; I ++) {try {logger. debug ("thread 2 is accessing the non-synchronous method !!! "); Thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace () ;}} logger. debug ("●●●●●● thread 2 exit non-synchronous method");} public static void main (string [] ARGs) {// This is the same object to be accessed by multiple threads. testsyna sameobj = new testsyna (); // thread 1 accesses the synchronous method and thread 2 accesses the non-synchronous method. thread T1 = new thread (New T1 (sameobj); thread t2 = new thread (New T2 (sameobj); t1.start (); t2.start ();}}
Running result:
0 [thread-0] Debug basic. B _syn.testsyna-start thread 1 ........... 0 [thread-1] Debug basic. B _syn.testsyna-start thread 2 ........... 1 [thread-0] Debug basic. B _syn.testsyna-▲▲▲▲▲▲method 1 for starting thread 1 to access synchronization method 1 [thread-1] Debug basic. B _syn.testsyna-●●●●●●● thread 2 starts to access non-synchronous method 1 [thread-1] Debug basic. B _syn.testsyna-thread 2 is accessing the non-synchronous method !!! 1001 [thread-1] Debug basic. B _syn.testsyna-thread 2 is accessing the non-synchronous method !!! 2001 [thread-1] Debug basic. B _syn.testsyna-thread 2 is accessing the non-synchronous method !!! 3001 [thread-1] Debug basic. B _syn.testsyna-thread 2 is accessing the non-synchronous method !!! 3001 [thread-0] Debug basic. B _syn.testsyna-▲▲▲▲▲▲method for removing thread 1 from synchronization 3001 [thread-0] Debug basic. B _syn.testsyna-exit thread 1 ........... 4001 [thread-1] Debug basic. B _syn.testsyna-thread 2 is accessing the non-synchronous method !!! 5001 [thread-1] Debug basic. B _syn.testsyna-●●●●●●● thread 2 exit non-synchronous method 5001 [thread-1] Debug basic. B _syn.testsyna-exit thread 2 ...........