Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Short answer is yes the following method is thread safe:</p> <pre><code>public static int CalcuateSomething( int number ) { int i = number * 10; return i; } </code></pre> <p>All local variables are safe provided they don't point to a shared object on the heap.</p> <p>Just as you have in the case of single thread applications the following invocations of this method will return different values.</p> <pre><code>CalculateSomething( 10 ) =&gt; 100 CalculateSomething( 20 ) =&gt; 200 </code></pre> <p>Why is that? Because each invocation of the method number takes on different values, and therefore i will too. The value of i is not remembered after the function is over because i is allocated on the stack. In almost all languages functions are model on a stack. Each time you invoke a different method the current method is paused. A new method is pushed onto the call stack and invoked. When that method is finished the program unpauses the calling method and resumes where it left off (i.e. at the returnAddress). Any local variable defined within that method is apart of that method's stackframe. The stackframe for our method above could be thought of like this:</p> <pre><code>public Class StackFrameForCalculateSomething implements StackFrame { public int ReturnAddress; public int i = 0; public int number; } </code></pre> <p>The stack could be thought of as a collection of StackFrame objects.</p> <p>Stack callStack</p> <p>Each time a new method is called the program might do something like the following: </p> <pre><code>StackFrameForCalculcateSomething s = new StackFrameForCalculateSomething(); s.returnAddress = instructionPointer; s.number = 10; callStack.push( s ); s.invoke(); StackFrameForCalculcateSomething s2 = new StackFrameForCalculateSomething(); s2.returnAddress = instructionPointer; s2.number = 20; callStack.push( s2 ); s2.invoke(); </code></pre> <p>What does this mean for threading? Well in the case of threads you'd have multiple independent callStacks with their own collection. The reason access to local variables is safe is because there's no way for Thread 1's callStack to get access to Thread 2's callStack because they are separate objects. Just like in the single thread case s and s2 are different objects with different values of number. And so they are independent from each other. Consider s was in Thread 1 and s2 is Thread 2. Thread 1 and Thread 2 don't share any memory in common so it's thread safe. </p> <p>Thread's don't share their stack frames. They do share the heap.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload