Thread-safe and insecure

Source: Internet
Author: User

First of all, to understand how threading works, the JVM has a main memory, and each thread has its own working memory, a thread to a variable operation, you have to build a copy in their working memory, After the operation is finished, write to main memory. When multiple threads operate the same variable at the same time, unpredictable results can occur. According to the above explanation, it is easy to think of the corresponding scenario.
The key to using synchronized is to set up a monitor, which can be a variable to be modified, as well as other objects that you think are appropriate, such as method, and then thread-safe by locking this monitor. After each thread obtains this lock, it will not release the lock it gets until it executes the process of load to mainmemory, Use&assign-workingmemory. This enables the so-called thread safety.

------------------------------------------------------

What is thread safety? How is thread safety done (principle)?
Thread safety means that multithreaded access to the same code does not produce indeterminate results. Writing thread-Safe code is low-reliance on thread synchronization.

------------------------------------------------------

In the interface mode, the thread has a shared data member, namely: private int count = 10;
In the inheritance mode, the threads have no shared members, but each thread has a private member, that is: private int count = 10;
The code of the landlord is just a good example of when you need to consider thread synchronization and how you can write code that is safe for threads in a certain program.
In a multithreaded environment, it must be thread-safe when threads do not share data. The problem is that this is rarely the case, and in most cases the data needs to be shared, and appropriate synchronization controls are required.

--------------------------------------------------------

Thread safety generally involves synchronized is a piece of code that can only have one thread at a time or the intermediate process may produce non-prefabricated results

---------------------------------------------------------

If your code is in a process where multiple threads are running at the same time, these threads may run the code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe.

For example, for example a ArrayList class, when adding an element, it may have two steps to complete: 1. This element is stored in the location of Items[size]; 2. Increase the value of Size.

In the case of single-threaded runs, if Size = 0, after adding an element, this element is at position 0, and size=1, and if it is in multithreaded situations, such as having two threads, thread A first stores the element in position 0. However, when CPU scheduler thread a pauses, thread B gets the chance to run. Thread B also adds elements to this ArrayList, since Size is still equal to 0 (note that we assume that adding an element is a two-step process, and thread A just completes step 1), so thread B also stores the element in position 0. Then both thread A and thread B continue to run, increasing the value of Size. Well, now let's take a look at ArrayList, where the element is actually only one, stored in position 0, and Size equals 2. This is "thread unsafe."


Summary: Thread safety-the result of a single thread executing on a shared code (global variable, static variable, etc.) is consistent with the simultaneous execution of multiple threads, and there is no ambiguity between the two;

Thread insecure-- the result of a single thread executing on a shared code (global variable, static variable, and so on) is inconsistent with the simultaneous execution of multiple threads, with two semantics,


Thread-safe and 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.