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 insecure".
Thread Safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is Thread-safe if it's manipulates shared data structures in a manner that guarantees safe exe Cution by multiple threads at the same time. There is various strategies for making THREAD-SAFE data structures. [1][2]
A program could execute code in several threads simultaneously in a shared address space where each of the those threads has ACC ESS to virtually all of the memory of every and other thread. Thread safety is a property, allows code to run in multi-threaded environments by re-establishing some of the CORRESPO Ndences between the actual flow of control and the text of the program, by means of synchronization.
1Sign in to vote
Hi
A pice of code is thread safe if it's guaranteed to work correctly during simultaneous execution
Http://en.wikipedia.org/wiki/Thread_safety
For instance if a method does something like this
void Method (Object myObject) { if (MyObject = = null) return; Do something string s = myobject.tostring (); MyObject = null; }
The code would explode if threads is executing the same method with the same object and one thread executes MyObject = Null After the other thread just verified it is ' t null, but before the other thread executes myobject.tostring. The first thread would get a NullReferenceException at myobject.tostring (), which wouldn ' t make much sense since apparant ly you just verified it wasn ' t null.
There is several techniques to prevent the from happening, most commonly using some form of lock to ensure only one thre Ad at a time executes a particular piece of code.
What is thread safety and thread insecure