Java multithreading: thread synchronization-experiment 1

Source: Internet
Author: User

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 ...........

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.