spiderman
spiderman

Reputation: 11112

Java Thread: Please clarify this understanding

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

  1. When invoking john.sayHi(), the Thread thread1 has acquired intrinsic lock of john object in-order to access the sayHi() method of john object.

  2. 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?]

  1. The thread thread1 is not running on any other object inside the JVM.

  2. 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

Answers (4)

ikegami
ikegami

Reputation: 385645

When invoking john.sayHi(), the Thread thread1 has acquired intrinsic lock of john object in-order to access the sayHi() 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

Karibasappa G C
Karibasappa G C

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

merlin2011
merlin2011

Reputation: 75545

  1. It will not run until it has acquired the lock.
  2. It is a separate thread of execution from the main thread, so it is "independent" in the sense that it is not tethered.
  3. Threads are represented by stacks and instruction pointers. There's a Thread object, the thread is not "running on it". The object is merely "holding state" for it.
  4. True.

Upvotes: 1

Alexey Malev
Alexey Malev

Reputation: 6533

  1. Almost. I'd replace "has acquired" with "will try to acquire".
  2. Don't know. Please explain what you mean by "running independently".
  3. Same as 2, but I'll try to guess - no, thread is not bound to any object except for related Thread object.
  4. Again, if I understood you correctly - the answer for all parts is yes, except for the remark about Thread object.

Upvotes: 2

Related Questions