Concurrent programming-Determining thread safety

Source: Internet
Author: User

Java's support for threading is like a double-edged sword, and thread safety can be very complex.in the absence of sufficient synchronization, the order in which operations are executed in multiple threads is unpredictable, and even strange results occur. It is the ability of each multithreaded developer to distinguish whether the code/program/class you write is thread-safe or whether the result of the code is always correct .


For the purposes of determining whether a class is thread-safe, here are a few things to consider


1. Whether the object will be accessed by multiple threads modified
If the object is accessed by multiple threads, such as a variety of context or factory, such as Hibernate in Sessionfactory.sessionfactory is the session's production plant, In general, the application of the library to the whole world only needs to maintain a sessionfactory, but the modification of its properties can be modified by multiple threads, so if there is not enough locking mechanism, sessionfactory is a thread unsafe object.


2. Note the static variable.
Notice the word variable,because a static variable belongs to that class and all object shares under that class, you can access/modify it directly through the class name, so in a multi-threaded environment. You can assert that all modifications to static variables are subject to thread safety. Unless the static variable is a concurrent container, the object is published by the delegate thread-safe container.


3. Method calls that change the internal state of the object.
Take a look at the 1th, synchronization is required when the properties of an object are modified by multiple threads.However, it does not mean that a setter method that does not provide a property can be reassuring. What really needs to be focused on is what internal method calls or externally exposed method calls can cause changes in the properties (States) of the object.


4. Single Case
I believe we all know that lazy loading singleton has a thread safety problem when learning programming.The first check after operation is a frequently ignored problem in multithreaded development. Because the variables shared by parallel programs are subject to change all the time, this involves the problem of data invalidation.The singleton is globally unique, so the singleton object is accessed by all threads, and if the attributes in the singleton allow modification, then a thread-safety issue is raised.when the object is singleton under spring management, when we are developing in the three-tier architecture. Declaring and manipulating global variables in DAO (Repository) or service also raises thread-safety issues.


Summary:
Unnecessary synchronization will result in performance loss, and the lack of a synchronization system will lose its correctness. Judging whether the class you are developing is thread-safe is a required course for every multithreaded developer.

Concurrent programming-Determining 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.