Java Multithreading--line Cheng

Source: Internet
Author: User

If there is more than one synchronized method inside an object, a single thread will be able to invoke one of the synchronized methods at a time, and the other threads can only wait, in other words, in a certain moment, There can only be one thread to access these synchronized method locks is the current object this, after being locked, no other thread can enter into the other synchronized methods of the current object
After adding a common method, we find that the synchronization lock is irrelevant.
After switching to two objects, it is not the same lock, the situation changes immediately.
Changes after the static synchronization method is changed
All non-static synchronization methods use the same lock-the instance object itself, that is, if the non-static synchronization method of an instance object acquires the lock, the other non-static synchronization methods of the instance object must wait for the lock to be released before the lock can be acquired. However, the non-static synchronization method of the other instance object because the non-static synchronization method with the instance object uses a different lock, the non-static synchronization method that waits for the instance object to acquire the lock can acquire their own lock without waiting for the lock to be released.

All static synchronization methods use the same lock-the class object itself, the two locks are two different objects, so there is no race condition between the static synchronous method and the non-static synchronization method. But once a static synchronization method acquires a lock, the other static synchronization methods must wait for the method to release the lock before acquiring the lock, regardless of whether the same instance object is between a static synchronization method or a static synchronization method of a different instance object, as long as they are the same class as the instance Object!

 PackageCom.company;/** Title: Judging the printed "one" or "" "? * * 1. Two common sync methods, two threads, standard print, print? One, 2. Add Thread.Sleep () to GetOne (), print? One, 3. New Common Method Getthree (), print? Three one, 4.  Two common sync methods, two number objects, print? Both one * 5.  Modify GetOne () for static synchronization method, print? Both one * 6.  Modify two methods are static synchronization method, a Number object? One, 7.  A static synchronization method, a non-static synchronization method, two number objects? Both one * 8.   Two static synchronization methods, two number objects? One Two * * Line Cheng key: *① non-static method of the lock default is this, the static method of the lock for the corresponding Class instance *② a moment, only a thread can hold a lock, no matter how many methods. */ Public classTestthread8monitor { Public Static voidMain (string[] args) { number number=NewNumber (); Number Number2=NewNumber (); NewThread (()Number.getone ()). Start (); NewThread (() {//number.gettwo ();Number2.gettwo (); }). Start ();//New Thread ((), Number.getthree ()). Start ();    }}classNumber {Static synchronized voidGetOne () {//Number.class        Try{Thread.Sleep (3000); } Catch(Interruptedexception e) {} System.out.println ("One"); }    synchronized voidGettwo () {// ThisSystem.out.println ("the"); }     Public voidGetthree () {System.out.println ("Three"); }}

Java Multithreading--line Cheng

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.