Understanding Multithreading Thread safety again (after understanding the Java memory model)

Source: Internet
Author: User

1. Multithreaded access to shared resources has a thread-safety issue,

No external access to the two shared resources.

1) Multi-threaded access to the method area data. There is a thread safety issue, by locking

2) Multi-threaded access to instance variables: When the object being accessed is a singleton, thread safety is present, and the object being accessed is a thread-safe case.

To say, static variables, instance variables, local variables in the multi-threaded security problem it!

(i) Verifying the thread safety of static variables:

(1) We can see from the diagram of the program execution that there is error data in the execution result, which proves that the static variable is a resource conflict problem.

(2) Program operation result diagram:

5, Conclusion: The static variable is also called the class variable, belongs to the class object all, is located in the method area, shares for all objects, shares one memory, once the value is modified, the other object is visible to the modification, therefore the thread is not safe.

(ii) Instance variables: Thread-safe, non-single-threaded, single-case

1. Instance variable: the instance variable belongs to the class object, that is, the object instance is private and is allocated in the heap of the virtual machine.

2. Verify thread security for instance variables:

(1) From the program, we can see that when the singleton mode, there is a resource conflict, when not a singleton mode, there will not be a thread conflict.

(2) Program operation result diagram:

Figure 1:

Figure 2:

3, the conclusion: the instance variable is the instance object private, the system only has one instance object, in the multi-threaded environment, if the value changes, then other objects are visible, therefore the thread is not safe, if each thread is executed in a different instance object,

The changes between objects and objects do not affect each other, so thread safety.

(c) Local variables: thread safety

1. Local variables: Variables defined inside the method.

2, verify the security of local variables:

(1) It can be seen from the program that local variables have no problem of resource conflict under multithreading

(2) Program operation result diagram:

3, Conclusion: Each thread executes the local variables in the memory space of the respective frame stack, and the threads are not shared, so there is no thread safety problem.

This article refers to http://longw.blog.51cto.com/6475045/1683360, thanks to the original author

Understanding Multithreading Thread safety again (after understanding the Java memory model)

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.