public class Qn {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
// recipient - the person who greets
public synchronized void sayHi(Friend recipient) {
System.out.format("%s" + " said hi to %s %n",
this.name, recipient.getName());
}
}
public static void main(String[] args) {
final Friend john = new Friend("John");
final Friend peter = new Friend("Peter");
new Thread(new Runnable() {
public void run() {
john.sayHi(peter);
}
}, "thread1").start();
}
}
Questions:
Please clarify the ones below if understanding is correct
When invoking
john.sayHi(), the Threadthread1has acquired intrinsic lock ofjohnobject in-order to access thesayHi()method ofjohnobject.The Thread
thread1is running independently in JVM.
I read these statements online, not sure what they mean! [How can a thread run on objects!!! Infact Threads execute the code, correct?]
The thread
thread1is not running on any other object inside the JVM.A thread never runs on any object. A thread is never executed by an object. A thread never runs on any other thread. A thread always run directly in JVM.