What is thread-safe and thread-insecure

Source: Internet
Author: User
Thread Safety is a multi-threaded access, with a locking mechanism, when a thread accesses one of the class's data, protection, other threads can not access until the thread is finished reading, other threads are available. There is no data inconsistency or data contamination.
thread Insecurity is not providing data access protection, it is possible to have multiple threads change data successively causing the resulting data to be dirty data

Causes of thread safety problems

1, the thread task processing to the shared data, 2, the thread task has multiple operations on the shared data, one thread is working on the shared data, the other thread is involved in the operation, resulting in data errors.

The solution:

As long as the code that guarantees multiple operations to share data goes to a time period, executed by a thread, other threads are not allowed to participate in the operation during execution.

How to ensure safe use of synchronized keyword synchronized (object) {code to be synchronized}

How to analyze whether a thread has security implications one, whether there is shared data in the thread task, and whether there are multiple lines of code to manipulate the shared data.

What is thread-safe and thread-insecure

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.