Java multithreading: thread synchronization-Experiment 5

Source: Internet
Author: User

original address:
http://blog.csdn.net/huang_xw/article/details/7318554

/*** @ Description: the instance of the stringbuffer object accessed by thread 1. When thread 2 wants to access this object, blocking will occur. * @ author Snoopy * @ blog http://blog.csdn.net/huang_xw */package basic. B _syn; import Org. apache. log4j. logger; public class testsyne {Private Static logger = logger. getlogger (testsyne. class); stringbuffer sb = new stringbuffer ("Initial string +"); // thread 1 static class T1 implements runnable {testsyne s; Public T1 (testsyne sameobj) {This. S = sameobj;} // thread 1 access method syn1 () Public void run () {logger. debug ("Starting thread 1 ........... "); S. syn1 (); logger. debug ("Exit thread 1 ........... ") ;}} // thread 2 static class T2 implements runnable {testsyne s; Public T2 (testsyne sameobj) {This. S = sameobj;} // thread 2 access method syn2 () Public void run () {logger. debug ("start thread 2 ........... "); S. syn2 (); logger. debug ("Exit thread 2 ........... ") ;}} public void syn1 () {string threadstr = thread. currentthread (). getname (); logger. debug ("▲▲▲▲▲▲" + threadstr + "Start access method syn1 ()"); try {synchronized (SB) {logger. debug (threadstr + "accessing object Sb"); sb. append ("string added by thread 1 +"); // simulate a time-consuming process thread with sleep. sleep (5000); logger. debug (threadstr + "sleep ends"); logger. debug ("the string is:" + sb. tostring () ;}} catch (interruptedexception e) {e. printstacktrace ();} logger. debug ("▲▲▲▲▲▲" + threadstr + "Exit method syn1 ()");} public void syn2 () {string threadstr = thread. currentthread (). getname (); logger. debug ("●●●●●●●" + threadstr + "Start access method syn2 ()"); sb. append ("string added by thread 2 +"); logger. debug ("the string is:" + sb. tostring (); logger. debug ("●●●●●●●" + threadstr + "Exit method syn2 ()");} public static void main (string [] ARGs) {// This is the same object to be accessed by multiple threads testsyne sameobj = new testsyne (); // thread 1 access synchronization method syn1 (), thread 2 also accesses the synchronization method syn2 (). thread T1 = new thread (New T1 (sameobj), "thread 1"); thread t2 = new thread (New T2 (sameobj), "thread 2"); t1.start (); // sleep is to ensure that thread 1 starts to run, and first accesses the stringbuffer instance. try {thread. sleep (50);} catch (interruptedexception e) {e. printstacktrace () ;}t2.start ();}}

Execution result:

 
0 [thread 1] Debug basic. B _syn.testsyne-start thread 1 ........... 0 [thread 1] Debug basic. B _syn.testsyne-▲▲▲▲▲method for accessing thread 1 syn1 () 0 [thread 1] Debug basic. B _syn.testsyne-thread 1 is accessing the object sb49 [thread 2] Debug basic. B _syn.testsyne-start thread 2 ........... 49 [thread 2] Debug basic. B _syn.testsyne-●●●●●●● thread 2 start to access method syn2 () 5000 [thread 1] Debug basic. B _syn.testsyne-thread 1 sleep ends 5001 [thread 1] Debug basic. B _syn.testsyne-at this time, the string is: initial string + String added to thread 1 + 5001 [thread 1] Debug basic. B _syn.testsyne-▲▲▲▲▲method of thread 1 exit syn1 () 5001 [thread 2] Debug basic. B _syn.testsyne-the character string is: initial string + String added to thread 1 + String added to thread 2 + 5001 [thread 1] Debug basic. B _syn.testsyne-exit thread 1 ........... 5001 [thread 2] Debug basic. B _syn.testsyne-●●●●●●● thread 2 exit method syn2 () 5001 [thread 2] Debug basic. B _syn.testsyne-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.