I am working on some concurrency programming and one part is bothering me.
Let’s say I have some class Foo that extends Thread and implements it’s own public method called bar() as well as the required run() method. If I implement multiple Foo objects, each one containing a reference to another Foo object, and inside the run() method for the Foo class is a call on the bar() method for whatever Foo object it has a reference to. If the Foo object with name “Thread-1” calls bar() on the Foo object with name “Thread-2“, then who is actually executing the method code in “Thread-2“?
Does execution get handed off from “Thread-1” to “Thread-2” to execute or does “Thread-1” continue executing code in “Thread-2“? If it is the second choice, how can I make it act like the first choice?
A new thread is started only if you invoke
thread.start()(or useexecutor.submit(runnable)). All other method invocations remain in the current thread.