Reputation: 11112
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 Thread thread1
has acquired intrinsic
lock of john
object in-order to access the sayHi()
method of john
object.
The Thread thread1
is 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 thread1
is 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.
Upvotes: 2
Views: 136
Reputation: 385645
When invoking
john.sayHi()
, the Threadthread1
has acquired intrinsic lock ofjohn
object in-order to access thesayHi()
method of john object.
More precisely:
When invoking john.sayHi()
, the Thread thread1
will wait until it can acquire a lock on john
before executing sayHi
. Once it has obtained the lock, it will execute sayHi
. It will release the lock when the method exits.
The Thread
thread1
is running independently in JVM.
Independently of what? Other threads? Yes, until it tries to obtain a lock. At that point, it can be impeded by other threads. When it has a lock, it can impede other threads.
The thread
thread1
is not running on any other object inside the JVM.
Threads run on CPUs, not objects.
Are you asking if a thread can execute more than one method in parallel? If so, the answer is no.
A thread never runs on any object.
Threads run on CPUs, not objects.
A thread is never executed by an object.
Threads aren't executed by anything. Threads execute code.
A thread never runs on any other thread.
Threads run on CPUs, not threads.
A thread always run directly in JVM.
The JVM has a virtual CPU on which the thread runs.
Upvotes: 2
Reputation: 2732
see below
When invoking john.sayHi(), the Thread thread1 has acquired intrinsic lock of john object in-order to access the sayHi() method of john object.
Ans -> any thread if it has to execute any synchronized method, then it will get the lock. object lock or class lock ? decided based on static or non static synchronized method. if its static then it takes class lock if non static then object lock is taken.
The Thread thread1 is running independently in JVM.
Ans -> if the thread is not deamon then it will be running indepndently. but if the thread is daemon then it depends on its parent thread , if parent dies it will also get terminated.
The thread thread1 is not running on any other object inside the JVM.
Ans -> thread will not run on any object in fact no code runs on any object. after all object holds the state of the execution thats it.
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.
Ans -> 100% true statement
let me know for any other doubts
Upvotes: 1
Reputation: 75545
Upvotes: 1
Reputation: 6533
Thread
object.Thread
object.Upvotes: 2