Some of your own insights on thread safety.

Source: Internet
Author: User

With regard to thread safety, we know that thread security is a problem that must be considered in multi-threading, but for reasons of thread insecurity, when we study data structures at university, we know that thread security is about modifying the same variable. Or first on Baidu's explanation:

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
That's what I understand. In the case of thread security is very simple, you, for example, you and another small partner on the Internet are going to you very much care about a thing to do to modify, add you two are simultaneously modified this value, then you think this value ultimately retained by you modified or your little partner modified it?
This is a more straightforward way of explaining, then in this case, if you want to modify the value is thread-safe, that is, you two can not modify the value at the same time, you think, if you are in the modification, your partner will wait for you to modify the completion of the changes before you can modify, Does this end up being the result of the changes you want to make?
Suffering in our computer, we all know that the computer is a process to exist, and each process has a lot of threads, then these threads to operate at the same time a variable in a computer, this variable if it is not thread-safe China that you can not predict
This variable is finally modified after the value.
So in our actual development, with our development of the Deputy Director of the word is that we can not use multithreading involves thread-safe code, if you really want to use the best in the JDK with the thread-safe class. Try to use sychronized these keywords as little as possible. Of course here you change the dressing
Know that the classes in the JDK are thread-safe??? First wrote here today,. Continue talking about thread safety tomorrow night. Came home from work to.

Some of your own insights on 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.