Why local variable streams in Java

I read multithreading in Java and I came across this

Local variables are thread safe in Java.

Since then, I have been thinking how / Why local variables are thread safe.

Can someone please let me know.

+85
java thread-safety
Oct 10 '12 at 18:22
source share
7 answers

When you create a thread, it will have its own stack. Two threads will have two stacks, and one thread will never share its stack with another thread.

All local variables defined in your program will be allocated on the stack in the stack (as Jatin commented, here it means a reference value for objects and a value for primitive types) (Each method call by a thread creates a stack frame on its own stack). As soon as the execution of the method is completed by this thread, the stack stack will be deleted.

There is a wonderful lecture by Stanford professor on youtube that can help you understand this concept.

+96
Oct 10 '12 at 18:22
source share

Local variables are stored in each thread of its own stack. This means that local variables are never shared between threads. It also means that all local primitive variables are thread safe.

public void someMethod(){ long threadSafeInt = 0; threadSafeInt++; } 

Local object references are slightly different. The link itself is not shared. The object referenced, however, is not stored in each thread of the local stack. All objects are stored in a common heap. If an object created locally never escapes the method in which it was created, it is thread safe. In fact, you can also pass it to other methods and objects if none of these methods or objects makes the transferred object available to other threads

+17
Oct 10 '12 at 18:25
source share

Think of methods like defining functionality. When two threads start the same method, they are in no way connected to each other. Each of them will create its own version of each local variable and will not be able to interact with each other in any way.

If the variables are not local (for example, instance variables defined outside the method at the class level), then they are bound to the instance (and not to a single run of the method). In this case, two threads working on the same method see one variable, and this is not thread safe.

Consider these two cases:

 public class NotThreadsafe { int x = 0; public int incrementX() { x++; return x; } } public class Threadsafe { public int getTwoTimesTwo() { int x = 1; x++; return x*x; } } 

In the first case, two threads running in the same instance of NotThreadsafe will see the same x. This can be dangerous because threads are trying to change x! In the second case, two threads running on the same Threadsafe instance will see completely different variables and cannot affect each other.

+14
Oct 10 '12 at 18:25
source share

In addition to other answers like Nambari's.

I would like to point out that you can use a local variable in the anoymous type method:

This method can be called on other threads, which may violate thread safety, so java forces all local variables used in anonymous types to be declared final.

Consider this illegal code:

 public void nonCompilableMethod() { int i=0; for(int t=0; t<100; t++) { new Thread(new Runnable() { public void run() { i++; //compile error, i must be final: //Cannot refer to a non-final variable i inside an //inner class defined in a different method } }).start(); } } 

If java allowed this (for example, C # does it through "closure"), the local variable is no longer thread safe in any circumstances. In this case, the value of i at the end of all threads is not guaranteed to be 100 .

+6
Oct 13
source share

Each method call has its own local variables and, obviously, the method call takes place in a single thread. A variable that is updated with only one thread is inherently thread safe.

However , pay close attention to what exactly is meant by this: only entries in this variable are thread safe; calling methods on the object that it refers to are not essentially thread safe . The same goes for updating object variables directly.

+5
Oct 10 '12 at 18:33
source share

The thread will have its own stack. Two threads will have two stacks, and one thread will never share its stack with another thread. Local variables are stored in each thread of its own stack. This means that local variables are never shared between threads.

+5
Apr 17 '13 at 7:08
source share

Basically, four types of storage are in java to store class information and data:

Method scope, heap, JAVA stack, PC

therefore, the Method and Heap areas are shared by all threads, but each thread has its own JAVA and PC stack and which is not used by other threads.

Each method in java is a stack frame. therefore, when one method is called by a thread, the stack stack is loaded into its JAVA stack. The entire local variable that is present in this stack stack and the associated operand stack is not shared by others. The PC will have information about the next instruction to execute in the byte method of the method. therefore, all local variables are SAFE MODES.

@Weston also gave a good answer.

+3
Dec 09 '14 at 6:56
source share



All Articles