Java Multithreading: Thread Synchronization--experimental 3__java

Source: Internet
Author: User
Tags sleep static class
Original address: http://blog.csdn.net/huang_xw/article/details/7318554
/** * @Description: Two threads (thread 1 and thread 2) access the internal synchronization method of the same object Syn1 () and synchronization Method Syn2 () * Results: Thread 1 accesses the synchronization method Syn1 () of the object sameobj, while thread 2 accesses the same in the object sameobj Step Method Syn2 () block.
 or thread 2 access, thread 1 is blocked. 
 * Conclusion: It is often known that threads are mutually exclusive when they access the same synchronization method for the same object. 
 * In fact, when different threads access different synchronization methods for the same object, the threads are mutually exclusive. * Change synchronization method to synchronized block the conclusion is also established.
 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 = 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 ("Start 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 ("▲▲▲▲▲▲ thread 1 begins accessing Sync Method 1");
			try {logger.debug ("Thread 1 is accessing sync Method 1!!!");
		Thread.Sleep (3000);
		catch (Interruptedexception e) {e.printstacktrace ();
	} logger.debug ("▲▲▲▲▲▲ thread 1 Exits sync Method 1");
		The//object's Sync Method 2 public synchronized void Syn2 () {logger.debug ("Thread 2 begins accessing Sync Method 2");
				for (int i = 0; i < 5; i++) {try {logger.debug (thread 2 is accessing sync Method 2!!!);
			Thread.Sleep (1000);
			catch (Interruptedexception e) {e.printstacktrace ();
	} logger.debug ("Thread 2 exits Sync Method 2");

		public static void Main (string[] args) {//This is the same object that multithreading will access 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 results:
0    [Thread-0] DEBUG Basic.b_syn. Testsync-Start thread 1
..... 1    [Thread-0] DEBUG Basic.b_syn. Testsync-▲▲▲▲▲▲ Thread 1 begins access to sync Method 1
1    [Thread-0] DEBUG Basic.b_syn. Testsync-Thread 1 is accessing sync Method 1!!!
1    [Thread-1] DEBUG Basic.b_syn. Testsync-Start thread 2
..... 3001 [Thread-0] DEBUG Basic.b_syn. Testsync-▲▲▲▲▲▲ thread 1 exits the Sync Method 1
3001 [Thread-0] DEBUG Basic.b_syn. Testsync-Exit Thread 1
..... 3001 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 begins access to sync Method 2
3001 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 is accessing sync Method 2!!!
4001 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 is accessing sync Method 2!!!
5001 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 is accessing sync Method 2!!!
6002 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 is accessing sync Method 2!!!
7002 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 is accessing sync Method 2!!!
8006 [Thread-1] DEBUG Basic.b_syn. Testsync-Thread 2 exits Sync Method 2
8007 [Thread-1] DEBUG Basic.b_syn. Testsync-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.