Java Multithreading: Thread Synchronization--experimental 5__java

Source: Internet
Author: User
Original address: http://blog.csdn.net/huang_xw/article/details/7318554
/** * @Description: Thread 1 Accesses an instance of the thread-safe object StringBuffer, which is blocked when thread 2 is to be accessed.

* @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 = 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 ("Start 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 + "being visited object SB");
				Sb.append ("Thread 1 added string +");
				Using hibernation to simulate a time-consuming process thread.sleep (5000);
				Logger.debug (Threadstr + "dormant End");
			Logger.debug ("At this time 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 ("Thread 2 added string +");
		Logger.debug ("At this time the string is:" + sb.tostring ());
	Logger.debug ("" + Threadstr + "Exit Method Syn2 ()");

		public static void Main (string[] args) {//This is the same object that multithreading will access Testsyne sameobj = new Testsyne ();
		Thread 1 accesses the synchronization method Syn1 () and 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 running first, and first accesses the StringBuffer instance.
		try {thread.sleep (50);
		catch (Interruptedexception e) {e.printstacktrace ();
	} t2.start (); }
}
Execution results:
0    [Thread 1] DEBUG Basic.b_syn. Testsyne-Start thread 1
..... 0    [Thread 1] DEBUG Basic.b_syn. Testsyne-▲▲▲▲▲▲ Thread 1 Start Access Method syn1 ()
0    [Thread 1] DEBUG Basic.b_syn. Testsyne-Thread 1 is accessing the object
SB   . [Thread 2] DEBUG Basic.b_syn. Testsyne-Start thread 2
.....   [Thread 2] DEBUG Basic.b_syn. Testsyne-Thread 2 Start Access Method Syn2 ()
5000 [Thread 1] DEBUG Basic.b_syn. Testsyne-Thread 1 hibernate end
5001 [Thread 1] DEBUG Basic.b_syn. Testsyne-At this point the string is: initial string + thread 1 joined the string +
5001 [thread 1] DEBUG Basic.b_syn. Testsyne-▲▲▲▲▲▲ Thread 1 Exit Method Syn1 ()
5001 [Thread 2] DEBUG Basic.b_syn. Testsyne-At this point the string is: the initial string + thread 1 joins the string + thread 2 joins the string +
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.