Java synchronized keyword usage and level of Lock: Method Lock, Object lock, Class lock

Source: Internet
Author: User
Tags instance method sleep

First of all explain: Method lock and Object lock is said to be a thing, that is, only method lock or object lock and class lock two kinds of locks


The author uses the easy language to explain the complex technology so thoroughly, learn, spread.

Original address: http://zhh9106.iteye.com/blog/2151791

In Java programming, often need to use synchronization, and the most used is probably the Synchronized keyword, the following look at the use of this keyword.

Since the Synchronized keyword involves the concept of a lock, it is important to understand some of the relevant lock knowledge first.

Java built-in locks: Each Java object can be used as a lock that implements synchronization, and these locks become built-in locks. The lock is automatically acquired when a thread enters a synchronized code block or method, which is released when the code block or method is exited. The only way to get a built-in lock is to enter the synchronized code block or method of the lock's protection.

A Java built-in lock is a mutex, this means that at most only one thread can acquire the lock, and when thread a attempts to obtain the built-in lock held by thread B, thread A must wait or block, knowing that thread B frees the lock and that a thread will wait forever if the B thread does not release the lock.

Java object locks and class Locks: Java object Locks and class locks are essentially the same as built-in locks in the concept of locks, however, the two locks are actually very different, object locks are used for object instance methods, or on an object instance, class locks are used for static methods of classes or for class objects of classes. We know that the object instance of a class can have many, but each class has only one class object, so object locks on different object instances are not disturbed, but each class has only one class lock. But one thing to be aware of is that the class lock is just a conceptual thing, not real, it's just used to help us understand the difference between a locked instance method and a static method.

The above has a little understanding of some of the concepts of the lock, the following discusses the use of the Synchronized keyword.

Synchronized usage: Synchronized cosmetic method and synchronized cosmetic code block.

The effects of both usages on object locks and class locks are analyzed below.

Synchronized decoration methods and code blocks for object locks: Java code    public class testsynchronized    {          public void test1 ()          {              synchronized (This)               {                    int i = 5;                    while ( i--  > 0)                    {                         system.out.println (Thread.CurrentThread (). GetName ()  +  "  :  "&nbsP;+ i);                         try                        {                               thread.sleep (       );                 }                        catch  ( Interruptedexception ie)                         {                         }                    }               }         }                public synchronized void test2 ()         {               int i = 5;               while ( i-- > 0)               {                    system.out.println (Thread.CurrentThread (). GetName ()  +  " : "  + i);                    try                   {                          Thread.Sleep (           );        }                   catch  (interruptedexception ie)                    {                    }               }         }                 public static void main (String[] args)         {              final  testsynchronized myt2 = new testsynchronized ();               thread test1 = new thread (  new  Runnable ()  {  public void run ()  {  myt2.test1 ();  }   },  "Test1"   );               thread test2 = new thread (  new runnable ()  {  public  void run ()  { myt2.test2 ();   }  },  "test2"   );               test1.start ();;        &NBSp;      test2.start ();    

Related Article

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.