Java synchronized memo

Source: Internet
Author: User
1   Public   Class Test {
2
3 Public Void Method0 (){ // ...} // Common functions
4
5 Public Synchronized Void Method1 (){ // ...} // Synchronous Functions
6
7 Public Void Method2 (){
8 Synchronized ( This ){ // ...} // Synchronization with the current object as the lockCodeBlock
9 }
10
11 Public Void Method3 (someobject so ){
12 Synchronized (So ){ // ...} // Synchronization code block with an object as the lock
13 }
14
15 Public Void Method4 (){
16 // ...
17 Private Byte [] Lock = New Byte [0]; // Special instance variables
18 Synchronized (Lock ){ // ...} // Synchronous Code Block
19 // ...
20 }
21
22 Public Synchronized Static Void Method5 (){...} // Static synchronization Functions
23
24 Public Void Method6 (){
25 Synchronized (Test. Class ){...}// Class literal is used as the synchronization lock code block.
26 }
27
28 }

1. synchronous block synchronized (this) {...} In Method1 and method2 ){...} the obtained synchronization locks are instance objects like test, that is, Object locks, so Method1 and method2 are equivalent.

2. When multiple concurrent threads access"Same"When the synchronization function or synchronization block in the object is obtained, the thread that acquires the object lock is executed. During the execution of this thread, other threads that want to access the object's synchronization function or synchronization block (whether or not the same synchronization function or synchronization block) will be blocked until the object lock is obtained, of course, the non-synchronous method to access the object or the thread of the synchronization block are not restricted by the object lock and can be accessed directly.

Iii. synchronized (this ){...} this indicates the object that calls this method. If the two threads call t1 and t2 (class test instantiation) respectively, the synchronization block is invalid for the two threads. You can use the synchronization block synchronized (SO) {...} In method3 ){...} method.

4. What if I want to synchronize some code in the function? In method4, a special instance variable acts as a lock.

5. Synchronized synchronized (test. class ){...} the obtained synchronization lock is a class lock, that is, the class test lock, rather than the class test object lock.

6. Because the class lock is different from the object lock, Method1 and method5 do not constitute synchronization in the multi-thread concurrent environment.

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.