Memory sharing between Threads

111 views Asked by At

I have a scenario where I am confused little bit. As per my knowledge every thread has some local memory.

// 100 elements in SomeArray
ArrayList SomeArray = new ArrayList();
 ForkJoinPool forkJoinPool = new ForkJoinPool(8);
        forkJoinPool.submit( () ->{
            SomeArray.parallelStream().map(e->{
              A a= new A();
              // perform some action
            });
        });
class A{

}

So for above code each thread will create a diff object of class A and they will work on that only. I don't think it should cause any data inconsistency. I have 2 cases here but I am not sure which one is right
Case 1- If both threads are sharing the same memory
Then suppose One Thread creates a object of class A with reference "a" and starts performing some action and then another thread creates a new object with same reference name but now "a" starts pointing to other memory in heap. As "a" is present in shared memory so now "a" will point to diff memory. So Actions that are being performed by thread 1 will start reflecting in other Object.
Case 2- If both threads are storing variable "a" in local memory
Then both threads will create diff objects and as "a" reference is present in local memory So there will be no overlap between them.
So in short will thread 1 "a" variable and thread 2 "a" variable will have same memory reference???

2

There are 2 answers

0
Nathan Hughes On

Threads do not have their own memory to use to store objects. But when a method executes, that method execution has its own stackframe allocated to it. The stackframe can store primitives and references to heap-allocated objects.

In your code the variable a is created within a block executed by a closure, a is a reference, it is on the stack frame and only the thread executing that closure can see it. Each time the closure executes it gets its own stackframe and its own a variable.

Each object of type A is created on the heap which is shared across threads. But only the current thread sees the local variables in a method executed by that thread.

(A closure can access fields declared in its environment. That means if you nested yet another closure inside the closure declaring a local a, then the nested closure could access that a, even if it would otherwise have gone out of scope. So you can create a situation with nested closures where more than one thread can see a local variable. But the posted code doesn't do that.)

0
Solomon Slow On

As per my knowledge every thread has some local memory.

There is no "memory" in the Java language. "Memory" is something that you only need to talk about if you are describing the internals of a JVM or, maybe, if you are writing native code in some other language such as C++ with which your Java code will communicate via the Java Native Interface.

The Java language has local variables, static variables, instances (a.k.a., "objects",) and fields within instances:

  • static variables are automatically available to any thread that executes code that has permission to access the variable.

  • Local variables cannot be shared between threads, *except* when an effectively final local variable is captured by a lambda expression, and the result of the lambda is shared between threads.

  • A field is shared if and only if the instance to which it belongs is shared.

  • Instances can be explicitly passed from one thread to another. The Runnable object that is used to start a new thread is automatically shared between the new thread and the thread that created it. Other instances can be made available for sharing via the fields of instances that already are shared. For example The shared Runnable object could have a field that refers to a BlockingQueue. The field would be shared because the Runnable is shared. The queue to which the field refers thus would be shared, and any object that is passed through the queue also becomes a shared object.