Synchronized summary of Java Multithreading Learning

Source: Internet
Author: User

0. Overview

   Synchronized is a built-in lock mechanism provided by Java to enable synchronous access to code blocks, calledbuilt-in lock (intrinsic lock)。 The built-in lock consists of two parts: one is a reference to the object as a lock, and the other is a block of code protected by this lock. It is necessary to understand thatsynchronized locks are references to objects, with only one built-in lock on the same object, and different objects have different built-in locks.

   The built-in lock of Java is a mutex, where the lock of an object can only be held by one thread at a time. Assuming thread a attempts to acquire a lock held by thread B, thread A is blocked, knowing that B releases the lock and a can hold the lock.if thread B never releases the lock, thread A also waits forever to form a deadlock. Since the thread waits for the built-in lock to be unable to be interrupted, this is a disadvantage of synchronized built-in lock, the requirement can be displayed lock Reentranlock solve, can refer to this blog:http://www.cnblogs.com/moongeek/p/7857794.html.

   The only way to get an object built-in lock is to enter a block of code that is protected by that object lock, and the only way to release the lock is to jump out of that code block. General synchronized use the following methods:

synchronized (lock) {    //  Access or modify shared status protected by this lock }
1, the use of synchronized

synchronized can modify a general method, a static method, a block of code, but whatever synchronized modifies, he gets a built-in lock on an object, and the unit of the lock is the object.

  1) Synchronized modifies the general method, the lock is the lock that holds the object of the method, and is mutually exclusive when accessing the same method of the same class.

 Public synchronized void dosomething () {         //  ...}

on code equivalent to:

 Public void dosomething () {     synchronized(this) {          //  ...     }}

  2) synchronized modifier code block, lock is the lock of the specified object, if it is the same object lock, then mutually exclusive access.

 //  lock for object,  synchronized   

3) synchronized modifies the static method, which is the lock of the class object, and all objects of that class are mutually exclusive when accessing the class.

 public  class   demo{ public  synchronized  static  void   DoSomething () {  ...  

on code equivalent to:

 Public class demo{    publicstaticvoid  dosomething () {        synchronized (Demo. ) Class) {            //  ...         }    }}    
2, re-entry sex

   When a thread requests a lock that is already held by another thread, the thread is blocked. But the built-in lock is reentrant, so if a built-in lock attempts to acquire a lock that is already held by himself, the request succeeds immediately. "Re-entry" means that the operation granularity of acquiring a lock is "thread" rather than "call".

  The re-entry is implemented by a lock and a count value associated with each. When the count value is 0 o'clock, the lock is considered not to be held by any thread. When a thread requests a thread that is not held, the JVM writes down the holder and increments the count by 1. If the same thread obtains this lock again, the count value is incremented, and the counter decrements when the thread exits the synchronization code block. When the count value is 0, the lock is released.

1  Public classTest {2     PrivateObject lock;3 4      Public synchronized voidsaysomething () {5         6     }7 8      Public synchronized voiddosomething () {9         //two functions are the same object lock, can be re-enteredTen saysomething (); One     } A      -      Public voidGosomewhere () { -         //object locks are not consistent and cannot be re-entered the         synchronized(lock) { - saysomething (); -         } -     } +}

Synchronized summary of Java Multithreading Learning

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.