简体   繁体   中英

How does java class method is allocated memory and executed in single threaded and multithreaded environment?

I read about how methods are executed and this is what I understand: 1) Methods are allocated memory in method area and only a single copy is maintained which is used across all the instances of the class. 2) When a method is called from an instance then the current thread(single threaded env) say main gets loaded and then stack is loaded with the method being called via instance.eg:

main(String ags[])
{
   A a = new A();
   a.method();
}
// code of method
 method()
 {
   for(int i=0;i<25;i++)
   system.out.println(i);
 }

so for this thread it has its own call stack and then on method call same method body with its local variables gets pushed onto the same stack above main method.

Now based on above understanding, what I dont understand is that in multi threaded environment how the same code will behave if I run two threads and both share the same instance. eg:

//My run method for myRunnable
run()
{
  a.method();
}

Thread one = new Thread(new myRunnable(a)); // object from above
Thread two= new Thread(new myRunnable(a));

Now when the two threads start executing they will have there own call stack.

How will the method of the shared object execute in this case?.

Thanks

1) Methods are allocated memory in method area and only a single copy is maintained which is used across all the instances of the class >> that means that the bytecode of method implementation is only one per all instances. And method bytecode memory region is separated from the object's heap.

Each thread has its own stack of course, just like you explain it.

If you have multiple threads running the same method on the same object concurrently, you have the following situation:

  • local variables are stored on each thread's stack. They are not shared and do not conflict.

  • The object instance ( this ) is stored on the heap, as well as all its fields (such as this.foo ). The heap is shared. To ensure that this works properly, you have to apply thread synchronization mechanisms as appropriate.

  • static fields are also shared and access must be coordinated, too

In your example, the i in the loop is a local variable. Both threads will print all of the numbers in sequence (but the output of the two threads is interleaved in an undefined order).

OK, you walk into a room.

Somebody hands you a clipboard and a pencil and a whiteboard marker, and then tells you to start following the instructions that are written on a certain poster on the wall.

There's a whiteboard on another wall: It looks like a spreadsheet with rows and columns, and numbers and words written in the cells. Your clipboard has a sheet of paper with more rows and columns, and some numbers are written in some of the cells in pencil.

The instructions tell you, step-by-step, how to perform some complex calculation. They say things like,

...
Step 37: Copy the number from B5 on the whiteboard into J2 on your
         clipboard.
Step 38: Add J2 through J7 on your clipboard, and write the result in J9.
Step 39: If the result in J9 is greater than the value in whiteboard-C9,
         then go back to step 22, otherwise, go on to step 40.
Step 40: Erase whiteboard-C9, and then copy the value from clipboard-J9
         into that location.
...

There's a space on your clipboard where you can write your own notes. You can use it, for example, to keep track of what step you're on, or whatever else you need to remember in order to get the job done.

There are other posters on the wall, and there are other people, each with his/her own clipboard. Some of the people are following instructions from the same poster as you, and some of them are reading from other posters. Everybody is reading from/writing to the same whiteboard.

Everybody is going at her/his own pace. The ones who are reading the same poster as you are not necessarily on the same step as you, and because each of you had different initial numbers written on your clipboards, you may not even be performing the instructions in the same sequence.

This is a simplistic model of multi-threaded computing: The posters on the wall are the methods, The whiteboard is the heap, the people are threads, and your clipboards are your stacks.

It's also, roughly similar to how scientific/engineering calculations were done during the industrial age. The people who did that kind of work were called "computers".

If you're coordinating the whole thing, and it's time to add a new "thread" (ie, when a new volunteer walks into the room), then you'll need to give that person his/her own clipboard (stack), with its own initial values (parameters), but you don't give the new person her/his own poster (methods): You just point her/him at one of the posters that already is up on the wall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM