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

7
  • static class Friend {, class cannot be static. Commented May 1, 2014 at 19:13
  • @Mr.Pandey Inner classes can certainly be static Commented May 1, 2014 at 19:14
  • Re "is running independently in JVM." Independently of what? Commented May 1, 2014 at 19:14
  • @Mr.Pandey Why do you think so? Have you heard about nested classes? Commented May 1, 2014 at 19:14
  • @user3580294:OOps! yes Indeed. Commented May 1, 2014 at 19:15

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 and many thanks.Thank you so much for taking this much effort. I appreciate for investing your time in making me understand. Almost all questions are clarified. I have one question - "Runs directly in JVM". What I thought is threads run in JVM. What I understand is Thread is running in CPU, Operating System is the scheduler. But has JVM has some control over these Threads?
What kind of control are you talking about? The only real form of control over a thread one has is when it's scheduled, and that's done by the scheduler. That's an OS function (which will be performed by the host OS in this case). A machine has memory and executes instructions, that's it.
(Scheduling can also be done in user-land in a co-operative multitasking system, but Java uses a preemptive multitasking system.)
Ok, clear. Infact when you mentioned Threads run on CPU's. I was puzzled what JVM is doing on Java threads because I thought Threads run in JVM. Ok, So Java Threads run on JVM [to be precise, they run on the virtual CPU in JVM]
2
  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.

4 Comments

2. "running independently". I meant to say it is running in JVM. But I now know that was an incorrect statement to make. Thankyou +1
All Java threads runs in JVM, but scheduled by OS scheduler together with JVM internal mechanisms. You're welcome, glad to help.
Thank you Alexey Malev for the last comment. To be more precise, let me take the words of @ikegami "The JVM has a virtual CPU on which the thread runs."
I am accepting @ikegami's answer as it is more descriptive and cleared all my questions, I appreciate your efforts and recognize you were the first one to answer.
1
  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.

1 Comment

Thank you for third point, +1. (4) is true, but phrasing is incorrect completely, and clarified by @ikegami
1

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

1 Comment

Thank you for third point. yes, right, no code runs on any object!. +1 But my fourth point is syntax wise incorrect, well explained by @ikegami

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.