Mechanism
The locking mechanism is used to solve the conflict problem of multi-threaded shared resources. Java associates an object lock for each object, usually dividing the locks into object locks and class locks, which are essentially object locks, except that object locks are associated with class object objects (Java.lang.Object), and class locks are associated with class objects Java.lang.Class.
The JVM has a count of each lock
- If the count is 0, the lock is not occupied and can be accessed by the thread holding
- An object lock for an object has been held by a thread, and the new thread will be suspended when it is accessed, knowing that it has been freed by the threads holding it and counting to 0
- A thread already holds a lock on an object that can be re-entered when the thread accesses the object lock again, and the count is +1
- When a thread frees an object lock, the count of the lock-1, when a lock is counted as 0 o'clock Lock is released, can be competed by the thread
Classification
- Regardless of how they are classified, the locks implemented by synchronized in Java are essentially object locks.
- There are usually two ways to implement the Java internal synchronization mechanism, synchronized the method of modification and the block of statements
- The Synchronized keyword is used for an object, which can be an instance object of a class or a class object
1 Public classSyncObject {2 //class Lock 1: Static method plus synchronous lock3 Public Static synchronized voidSyncMethod1 () {4 Try{5System.out.println ("testMethod1 start!");6Thread.Sleep (3000);7}Catch(Interruptedexception e) {8E.printstacktrace ();9}TenSystem.out.println ("testMethod1 end!"); One} A - Public voidSyncMethod2 () { - //Class Lock 2: Pass class type parameters through a synchronous statement block the synchronized(SyncObject.class) { - Try{ -System.out.println ("testMethod2 start!"); -Thread.Sleep (3000); +}Catch(Interruptedexception e) { -E.printstacktrace (); +}finally{ ASystem.out.println ("testMethod2 end!"); at} -} -} -}
Class locks Act on class objects, and for a class, no matter how many instances of the class, its static and static methods are only one copy, saved in the class object. A synchronous statement or synchronous method that is implemented by locking a class object, regardless of which instance of the object is accessed, is required to compete for this type of lock
1 Public classSyncObject {2 //Object Lock 1 (Method Lock): By method plus synchronous lock3 Public synchronized voidSyncMethod1 () {4 Try{5System.out.println ("testMethod1 start!");6Thread.Sleep (3000);7}Catch(Interruptedexception e) {8E.printstacktrace ();9}TenSystem.out.println ("testMethod1 end!"); One} A - Public voidSyncMethod2 () { - //Object Lock 2: Pass the object type parameter through a synchronous statement block the synchronized( This) { - //The parameter here can be this instance, or another instance such as New SyncObject (), which instance will be added to the lock - Try{ -System.out.println ("testMethod2 start!"); +Thread.Sleep (3000); -}Catch(Interruptedexception e) { +E.printstacktrace (); A}finally{ atSystem.out.println ("testMethod2 end!"); -} -} -} -}
An object lock locks the instance object of the class, and each new operation obtains a fresh instance object, and each instance object has its own lock for a synchronized decorated non-static method or a statement block that passes arguments to an instance object. TIPS
-
The scope of the synchronized is the object
- Synchronized-modified blocks of code need to pass in an object parameter (this object can make a Class object), which is the scope of the object
- The synchronized modified non-static method is scoped to this, which is the current object instance. When modifying a static method, the scope is this class, that is, class object
- Synchronized of the particle size needle can not be divided
Because the lock is for the entire object, the lock is not held by other threads when the lock of an object is held
- When a thread calls an object A's Synchronized method (A.SYNCMETHOD1), the object's other synchronized methods (A.SYNCMETHOD2.) will not be called until the method ends. Synchronous code block synchronized (a) has the same period of action as the A.SYNCMETHOD1
- When a thread calls the static synchronized method of a Class A (A.STATICSYNCMETHOD1) before the method ends, the class's other static Synchronized method (A.STATICSYNCMETHOD2. ) will not be called
- When a thread calls an object A's Synchronized method (A.SYNCMETHOD1) Before the method ends, other static synchronized methods of the Class (A.SYNCMETHOD2.) can also be invoked, and vice versa.
-
The synchronized only locks the modules it modifies.
- A module that is not synchronized modified can be accessed asynchronously because the thread does not need to acquire an object lock when it accesses it
Java Object Lock Learning