• Post Reply Bookmark Topic Watch Topic
  • New Topic

how method.invoke works?  RSS feed

 
Suat Ugurlu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anyone know what exactly happens
When an object invokes another object's method. (Methodname.invoke)?
Suppose a mothed is being executed by the class itself while another
object invokes one of this class's methods.
Does the class is interrupted at that time by the JVM? or something else
happens?
Regards,Suat.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class cannot be interrupted, since it is an abstract concept.
If

the method jp1.repaint () isn't affected by jp2.repaint, since jp1 and jp2 are two different objects.
You may buy two cheeseburgers and put some extra ketchup on the one - it will not affect the other cheeseburger. Therefore it isn't of interest, who is putting this extra ketchup on the cheeseburger.
The objects are mostly some values in the memory - a method is a regulation how to deal with these values. You may act on the sepearated values with the same ruleset, even if there are thousands of objects.
It's getting different when you deal with class-variables, alias static variables.
There is only one variable, shared for every object of that class you create. Then you may need some synchronisation for that variable - lock it, unlock it. There are a lot of threads to this issue in the intermediate/ beginner/ Threads - forums.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several things at play here. The class contributes the byte-code which is executed by the JVM. Usually the JVM fetches one instruction, executes it, fetches the next, executes it and so on.
You were asking about two things running at the same time which means threads. Each thread keeps track of its place in the instructions. They don't really run "at the same time" but one thread runs a few instructions, then another runs a few, then another runs a few. Two threads can have instruction pointers in the same method, but they're pretty nicely isolated from each other and they can step through the instructions without causing too much confusion.
Until they start accessing variables! If two threads are changing and reading the same variables in memory they get into trouble. This can happen with static variables or if you pass the same object parameter two threads or any number of ways two threads can get references to the same object.
If that got your interest at all, look for the Sun tutorial on threads. It's good stuff! There is a threads forum up the page a way if you want to get into this in more detail.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!