Thread Safety and Shared Resources
Code that is safe to call by multiple threads simultanously is called thread safe. If a piece of code is the thread safe, then it contains no race conditions. Race condition only occur when multiple threads update shared resources. Therefore It is important to know what the resources Java threads share when executing. Local Variables
Local variables is stored in each thread ' s own stack. That means that local variables is never gkfx between threads. That also means, all local primitive variables is thread safe. Here's an example of a thread safe local primitive variable:
public void SomeMethod () {
long threadsafeint = 0;
threadsafeint++;
}
Local Object References
Local references to objects is a bit different. The reference itself is not shared. The object referenced however, is not a stored in each threads ' s local stack. All objects is stored in the shared heap. If an object created locally never escapes the method it's created in, it's thread safe. In fact can also pass it on to other methods and objects as long as none of these methods or objects make the passed O Bject available to other threads. Here's an example of a thread safe local object:
public void SomeMethod () {
Localobject localobject = new Localobject ();
Localobject.callmethod ();
METHOD2 (Localobject);
}
public void Method2 (Localobject localobject) {
localobject.setvalue ("value");
}
The Localobject instance in this example are not returned from the method, nor are it passed to any other objects that AR e accessible from outside the SomeMethod () method. Each thread executing the SomeMethod () method would create its own Localobject instance and assign it to the Localobject re Ference. Therefore the use of the Localobject are thread safe. In fact, the whole method SomeMethod () is the thread safe. Even if the Localobject instance is passed as parameter to other methods in the same class, or in other classes, the use O F It is thread safe. The only exception are of course, if one of the methods called with the Localobject as parameter, stores the Localobject in Stance in a-allows access to it from the other threads. Object Members
Object members is stored on the heap along with the object. Therefore, if threads call a method on the same object instance and this method updates object members, the method is Not thread safe. Here's an example of a method, that's not thread safe:
public class notthreadsafe{
StringBuilder builder = new StringBuilder ();
Public Add (String text) {
this.builder.append (text);
}
}
If Threads Call the Add () method simultanously on the same Notthreadsafe instance then it leads to race Condi tions. For instance:
Notthreadsafe sharedinstance = new Notthreadsafe ();
New Thread (New Myrunnable (Sharedinstance)). Start ();
New Thread (New Myrunnable (Sharedinstance)). Start ();
public class Myrunnable implements runnable{
Notthreadsafe instance = null;
Public myrunnable (Notthreadsafe instance) {
this.instance = instance;
}
public void Run () {
This.instance.add ("some text");
}
}
Notice how the Myrunnable instances share the same Notthreadsafe instance. Therefore, when they call the Add () method on the Notthreadsafe instance it leads to race condition.
However, if threads call the Add () method simultanously on different instances then it does not leads to RAC E condition. The example from before, but slightly modified:
New Thread (New Myrunnable Notthreadsafe ()). Start ();
New Thread (New Myrunnable Notthreadsafe ()). Start ();
Now the threads has each their own instance of Notthreadsafe so their calls to the Add method doesn ' t interfere with Each of the other. The code does not has race condition anymore. So, the even if an object was not a thread safe it can still was used in a by that doesn ' t leads to race condition. The Thread Control Escape Rule
When trying to determine if your code ' s access of a certain resource are thread safe you can use the thread control escape Rule
If A resource is created, used and disposed within the
control of the same thread, and never escapes the
control O F This thread, the use of the.
resource is thread safe.
Resources can is any GKFX resource like an object, array, file, database connection, socket etc. In Java you does not always explicitly dispose objects, so "disposed" means losing or null ' ing the reference to the object.
Even if the use of a object is thread safe, if that object points to a shared resource like a file or database, your APPL Ication as a whole may is not a thread safe. For instance, if thread 1 and thread 2 each create their own database connections, connection 1 and connection 2, the use of each connection itself is thread safe. The use of the database, the connections point, is not a be thread safe. For example, if both threads execute code like this:
Check if record x exists
if not, insert record X
If threads execute this simultanously, and the record X they am checking for happens to being the same record, there is A risk that both of the threads end up inserting it. How:
Thread 1 Checks if record X exists. Result = no
Thread 2 checks if record X exists. Result = no
thread 1 inserts record x
thread 2 inserts record X
This could also happen with threads operating on files or other shared resources. Therefore It is important to distinguish between whether an object controlled by a thread is the resource, or if It merely references the resource.
Http://tutorials.jenkov.com/java-concurrency/thread-safety.html