"Java Thread safety and immutability"

Source: Internet
Author: User

Original link

Jakob Jenkov Translator: Gao song proofreading: Ding

In Java, a race condition occurs when multiple threads access the same resource at the same time, and one or more of the threads writes to the resource. Multiple threads reading the same resource at the same time will not produce a race condition.

We can achieve thread safety by creating immutable shared objects to ensure that objects are shared between threads without modification. The following example:

    1. public class immutablevalue{
    • private int value = 0;
    • public immutablevalue (int value) {
    • This.value = value;
    • }
    • public int GetValue () {
    • return this.value;
    • }
    • }

Copy Code

Note that the member variable value of the Immutablevalue class is assigned by a constructor and does not have a set method in the class. This means that once the Immutablevalue instance is created, the value variable can no longer be modified, which is immutability. But you can read the value of this variable by using the GetValue () method.

( Translator Note: Note that the "invariant" (immutable) and "Read Only" (read only) are different. When a variable is "read-only", the value of the variable cannot be changed directly, but it can change when other variables change. For example, a person's date of birth is a "constant" attribute, and a person's age is a "read only" attribute, but is not the "invariant" attribute. As time changes, the age of one person changes, and the day of birth of a person does not change. This is the difference between "unchanging" and "read-only". (Excerpt from the 34th chapter of Java and Patterns)

If you need to manipulate an instance of the Immutablevalue class, you can do so by creating a new instance after the value variable is obtained, and here is an example of an addition to the value variable:

    1. public class immutablevalue{
    • private int value = 0;
    • public immutablevalue (int value) {
    • This.value = value;
    • }
    • public int GetValue () {
    • return this.value;
    • }
    • Public immutablevalue Add (int valuetoadd) {
    • return new Immutablevalue (This.value + valuetoadd);
    • }
    • }

Copy Code

Note that the Add () method returns the result of the addition operation as a new instance of the Immutablevalue class, rather than directly manipulating its own value variable.

References are not thread safe!

It is important to remember that even if an object is a thread-safe immutable object, references to that object may not be thread-safe. See this example:

    1. public void calculator{
    • Private Immutablevalue currentvalue = null;
    • Public Immutablevalue GetValue () {
    • return currentvalue;
    • }
    • public void SetValue (Immutablevalue newvalue) {
    • This.currentvalue = newvalue;
    • }
    • public void Add (int newvalue) {
    • This.currentvalue = This.currentValue.add (NewValue);
    • }
    • }

Copy Code

The Calculator class holds a reference to the Immutablevalue instance. Note that this reference may be changed by the SetValue () method and the Add () method. Therefore, even though an immutable object is used inside the calculator class, the Calculator class itself is mutable, so the calculator class is not thread-safe. In other words: The Immutablevalue class is thread-safe, but the class that uses it is not. This is something to keep in mind when trying to get thread safety through immutability.

For the calculator class to be thread-safe, declare the GetValue (), SetValue (), and Add () methods as synchronous methods.

"Java Thread safety and immutability"

Related Article

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.