Java Learning (11): Java lock synchronized, object lock and Class lock example

Source: Internet
Author: User

Java locks are divided into object locks and class locks.

1. When two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed for that object within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block.

2. However, another thread can still access the non-synchronized (this) synchronous code block in the object.

  3. In particular, when a thread accesses a synchronized (this) synchronization block of object, the access of other threads to all other synchronized (this) synchronization blocks in the object is blocked.

4. Synchronous locking is the object, not the code. Therefore, if you have a synchronization method in your class, this method can be executed by two different threads at the same time, as long as each thread creates an instance of the class itself.

5. The Synchronized method of different object instances is non-interfering. In other words, other threads can access the Synchronized method in another object instance of the same class at the same time.

6 The . Synchronized keyword cannot be inherited, that is, the method of the base class synchronized F () {} is not automatically synchronized F () {} in the inheriting class, but instead becomes F () {}. Inheriting a class requires that you explicitly specify one of its methods as the Synchronized method.

7. When a global object or class is locked, all objects of that class function.

Class Lock Example

To lock a global variable:

1  Public classMysynchronizedextendsThread2 {3     Private intVal;4     5     Private StaticObject lock =NewObject ();6     7      PublicMysynchronized (intv)8     {9val =v;Ten     } One      A      Public voidPrintval (intv) -     { -         synchronized(Lock) the         { -              while(true) -             { - System.out.println (v); +             } -         } +     } A      at      Public voidRun () -     { - Printval (val); -     } -}
View Code

Lock the entire class:

1  Public classMysynchronizedextendsThread2 {3     Private intVal;4     5      PublicMysynchronized (intv)6     {7val =v;8     }9     Ten      Public voidPrintval (intv) One     { A         synchronized(mysynchronized.class) -         { -              while(true) the             { - System.out.println (v); -             } -         } +     } -      +      Public voidRun () A     { at Printval (val); -     } -}
View Code

Additional Lock Examples:

1  Public classMysynchronizedextendsThread2 {3     PrivateString name;4     5     PrivateString Val;6     7      Publicmysynchronized (string name, String v)8     {9          This. Name =name;Tenval =v; One     } A      -      Public voidPrintval () -     { the         synchronized(val) -         { -              while(true) -             { +SYSTEM.OUT.PRINTLN (name +val); -             } +         } A     } at      -      Public voidRun () -     { - printval (); -     } -      in      Public Static voidMain (String args[]) -     { toMysynchronized F1 =NewMysynchronized ("Foo 1:", "Printval"); + F1.start (); -mysynchronized F2 =NewMysynchronized ("Foo 2:", "Printval"); the F2.start (); *     } $}
View Code

The particularity of a string constant, belonging to the same object.

Java Learning (11): Java lock synchronized, object lock and Class lock example

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.