I had read this question
Static method behavior in multi-threaded environment in java.
But this question didn't answer that can multi-thread running the same method at the same given time in a multicore processor enviroment.
If my title question is "yes", how exactly does my computer implement this? Since there is only one static method waiting to be called.
Do two cores respectively copy that static method to their ram of core and run them simultaneously and respectively?
I don't know if I express my point clearly. If not, I am more than willing to explain it. Thank you.
-
multicore or not, this is the same RAM. Answer to your question is the same as the one you're referring to. – Jean-François Fabre Sep 13 '16 at 08:28
3 Answers
All of the information required to execute a piece of code independently is stored locally in a thread. When a core executes a method it does so in the context of a thread. Since the actual executable code (the instructions) cannot be changed (it is not writeable), there is no need to copy it anywhere: it can just be shared between threads/cores.
Methods can have local variables. These are stored on the stack, which is a property of the thread. Each thread has a separate piece of memory for its stack, and so each core will be accessing different memory for local variables.
For data on the heap, this is shared, which is why you have to take care when accessing that in a multi-threaded environment by co-ordinating access (for example, by using synchronized
).
Being static or not static is not relevant. In either case, there is only one copy of the code. A non-static method can have multiple instances of the object data (and so potentially different cores will be accessing different heap data, although not necessarily) and a static method accesses static data (which is shared, and you have to co-ordinate that).
A core may copy executable code to a local cache for execution, but this is done for performance reasons and not because of any issues with shareability.
Quick answer: yes.

- 8,529
- 8
- 43
- 62
-
It looks like I misunderstand what methods are,,, So, when a thread is created, it stores all context of a static method into itself. Then when that thread is executing that method, it justs run the context which has been already stored in itself??? Is that correct? – Lion Lai Sep 13 '16 at 10:24
-
1Yes - the thread holds the context; that is, where you are in the code and the values of local variables in both static and instance methods. Everything else is on the heap. The only difference between static and instance methods is that an instance method can also access the members of an object whereas a static method cannot. It is confusing as instance methods look like they "exist" per object, but really the only thing that is exists in the object is the data. There is only one copy of the code. – rghome Sep 13 '16 at 12:44
If you are talking about multithreading in single process then two threads will run a static method based on thread priority . But if you are talking about Multi Processor Multi Threading enviorment then Yes the same Static or non static method at the same given time can run in a multicore processor enviroment. Because each process will Run into Different JVM enviorment.

- 142
- 2
- 10
Short Answer, yes its possible.
Each method call will get its own stack and can run in parallel.
The static method to be called is just a set of executable statements. Since threads are not allowed to write to the method logic, each thread refers to the same set of instructions. It also has a pointer (sort of) which tells which line its executing from the instruction.
Think of it from a class/object perspective. All objects of same class share the same code base, but does that mean 2 objects cannot be created at the same time?
The instance refers to the same object but yet is different than another instance.
Same way, 2 method calls refer to same set of instructions, but differ in data ( which instruction they are on, what is the value of local variables etc).
I think your confusion is because the method is static (hence your assumption, only one copy). The copy remains the same, doesnt mean every time a method is called, instructions have to be copied. Differentiate between instruction and data, that will help.

- 670
- 4
- 10