6

We cannot call Runnable's run() method directly on thread's object but according to the below program we are doing it without any compilation or Runtime errors. Why is it so?

public class ThreadCheck implements Runnable {

    @Override
    public void run() {
        for (int i=0; i<10; ) {
            System.out.println(++i);
        }
    }

    public static void main(String[] args) {
        Thread mythread = new Thread(new ThreadCheck());
        mythread.run();
        mythread.run();
        mythread.start();
    }   
}

Output: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Prateek Kar
  • 61
  • 1
  • 2

4 Answers4

9

This will not have any compilation or run-time errors. But it also will not spawn new threads. It will execute the run() method in the current main thread.

refer what will happen if we directly call run method?

Community
  • 1
  • 1
KDP
  • 1,481
  • 7
  • 13
1

We can call Runnable's run() method directly on thread's object. But, when you call run() method from thread's object, the run() method will work as a normal method. When you call it, it doesn't create a new thread.

In this case the run() method will be treated as a normal method call. But when you call the start() method on the Thread object, then it automatically calls the run() method on a new thread in the JVM.

Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
Puneet Chawla
  • 5,729
  • 4
  • 16
  • 33
  • When you call `t.start()` that _also_ works as a normal method. Each method runs _in_ the thread that calls it, and each method does what it is supposed to do. `t.start()` is supposed to start a new thread. What is `r.run()` supposed to do? Don't ask me! Ask the person who wrote it! – Solomon Slow Jun 24 '15 at 17:55
  • Yes, Every method runs in the thread that calls it. Every thread starts in a separate call stack. When call run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack. – Puneet Chawla Jun 25 '15 at 15:04
  • You can call run() method directly but there will be no context -switching between two threads. Ex. You have two objects of same class t1 & t2 and we call run() method from both object. We specified Thread.sleep(500) in run() method and prints 1 to 5 by looping. Ouput will be 1 2 3 4 5 1 2 3 4 5. Because t1 & t2 will be treated as normal object not thread object. According to my understanding, above description are correct, If i am wrong then explain it. – Puneet Chawla Jun 25 '15 at 15:05
  • There's nothing wrong with your _facts_, but the way you talk about them sounds like magical thinking. You said, "In this case the run() method will be treated as a normal method call." But that's because in _every case_ the run() method is treated as a normal method call. All method calls in Java are "normal." You say, "...treated as normal object, not thread object..." I think that if you had a deeper understanding of stacks and threads and how method calls actually work, then threads would seem less magic---It would _all_ just be "normal". – Solomon Slow Jun 25 '15 at 16:12
  • Ok. Thanks James large. – Puneet Chawla Jun 25 '15 at 16:17
0

To create/spawn a custom new thread, two things are necessary:

  • Create a new Thread object (call it thread) with the argument as object reference of the custom thread (myThread in this case)

  • Call thread.start();

run() can be called directly, but the sequence of executions in the run method will not happen in a new thread! The sequence of executions will take place in the existing thread for the main method.

Nirmal
  • 1,229
  • 1
  • 15
  • 31
0

We cannot call Runnable's run() method directly on thread's object

That is not true. Your example proves that you can call it. But maybe it does not do what you think it does.

See Two thread which invokes wait and notify for a simple explanation of the relationship between the Thread class and the Runnable interface.

Community
  • 1
  • 1
Solomon Slow
  • 25,130
  • 5
  • 37
  • 57