Java Concurrent Programming Learning Note (i)--thread safety

Source: Internet
Author: User

1. When multiple threads access a state variable and one of the suggestions Cinderella performs a write operation, the synchronization mechanism must be used to coordinate the access of these threads to the variable. The main synchronization mechanism in Java is the keyword synchronized, which provides an exclusive locking method.

2. In any case, the thread-safe class is meaningful only if the class contains only its own state.

3, when more than one thread accesses a class, regardless of the runtime environment of how to use the scheduling method or the suggestions Cinderella How to alternate execution, and in the keynote code does not require any additional synchronization or coordination, this class can show the correct behavior, then it is called this class is thread-safe.

4, stateless objects must be thread-safe.

5, atomic operation means that for all operations that access the same state (including the operation itself), this operation is an atomic operation.

6. Use existing thread-safe objects (atomic variables) (such as Acomiclong) to manage the state of the class as much as possible. It is easier to judge the possible state transitions of thread-safe objects than non-thread-safe objects, making it easier to maintain and validate thread security.

7, to maintain the consistency of the state, you need to update all the relevant state variables in a single atomic operation.

8. Each Java object can be used as a lock for synchronization, known as a built-in lock (intrinsic lock) or monitor lock. The thread automatically obtains the lock before it enters the synchronization code block and automatically releases the lock when it exits the synchronization code block.

9. Atomicity in concurrent environments has the same meaning as atomicity in transactional applications-a set of statements is executed as an indivisible unit. Any thread that executes a synchronous block of code cannot see that another thread is executing a synchronous block of code that is protected by the same lock.

10, re-enter, the granularity of the operation to get the lock is "thread", not "call". The built-in lock (Synchronized Block) is reentrant.

11. Each shared and variable variable should be protected by only one lock, so that the maintainer knows which lock it is.

12. For each invariant condition that contains multiple variables, all of the variables involved need to be protected by the same lock.

13, eldest brother to perform a long time calculation or may not be able to complete the operation of a quick look, be sure not to hold the lock.

Java Concurrent Programming Learning Note (i)--thread safety

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.