1

I am not able to understand the behavior of this below program ,well as i can see that there is no thread reference is there in which we can pass the myThread reference but still the program is executing please advise is it the main thread which is executing this program

class MyThread extends Thread 
{
    public static void main(String [] args) 
    {
        MyThread t = new MyThread();
         t.start();
        System.out.print("one. ");
         System.out.print("two. ");
    }
    public void run() 
    {
        System.out.print("Thread ");
    }
}

the output is

one. two. Thread 
roeygol
  • 4,908
  • 9
  • 51
  • 88
  • You should add some pause, those statements are to small (in time). Add a `Thread.sleep(200)` just after the start to simulate a process, you will see that the thread is running. But the OS is not giving the hand quickly enough here so the main thread have the time to end. For more information, you should read how really works threads in term of CPU – AxelH Jan 19 '17 at 13:39
  • Everything totally as expected. MainThread executes `main`, creates Thread t and starts it, then outputs one. two. Thread t outputs Thread. When it gets scheduled for execution is a different story. But in a Monitor (when debugging) you will clearly see, that there is a separate thread executing. – Fildor Jan 19 '17 at 13:42
  • 4
    "well as i can see that there is no thread reference is there in which we can pass the myThread reference but still the program is executing" -- what does this mean? Please clarify your question. In any case, `MyThread` is a subclass of `Thread` so it's a `Thread`. And you're calling the `Thread.start()` method on it. So it executes. It's not clear to me what you expected to happen differently. – Erwin Bolwidt Jan 19 '17 at 13:43
  • You can add to your output `Thread.currentThread().getName()` to check on which Thread it is executed. for example: `System.out.print("one. from " + Thread.currentThread().getName());` – Fildor Jan 19 '17 at 13:45
  • Have a look at this post for complete workflow : http://stackoverflow.com/questions/8579657/whats-the-difference-between-thread-start-and-runnable-run/35931153#35931153 – Ravindra babu Jan 19 '17 at 14:10

3 Answers3

1

This program consists of two threads.

  1. The main() thread which is started when you start the program
  2. The MyThread thread which you start from main()

This call:

t.start();

...will start a 2nd thread, which will run the code in the MyThread class's run method.

john16384
  • 7,800
  • 2
  • 30
  • 44
0

Because when you call t.start() , the run() method (you override) is executed,

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

The start() method :

- Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
gaston
  • 498
  • 7
  • 15
  • Please, explain the problem of overriding `run` – AxelH Jan 19 '17 at 13:45
  • @AxelH the start() method in the Thread class calls its run() method, since MyThread extends Thread and overrides Thread's version of run(), your run() method will be called. – Luciano van der Veekens Jan 19 '17 at 13:47
  • Since it is unclear what exactly OP does not understand, it is evenly unclear how this answer can contribute to the OP's understanding of threads, I would say. Outside that - I do not see the OP asking a "Why ... " Question that could be answered with an answer that starts with "Because ... " ;) – Fildor Jan 19 '17 at 13:50
  • 1
    @AxelH Why would he explain that? I don't see gaston mentioning anywhere that there is a problem with overriding `run` – Erwin Bolwidt Jan 19 '17 at 13:51
  • Ok, I had understand the question differently than everyone ... so I was looking for a problem in the code. I didn't saw why overriding the `run` could be the reason of the unexpected behavior in OP code. For me, the OP understand Threads but didn't get why his output are not in an expect order (`Thread one two`). PS : my comment was there to improve the answer, I am well aware of how this works. – AxelH Jan 19 '17 at 13:57
0

I've always found the practice of letting your Main class extend something (in example code) dubious at best as it is unclear for beginners that when the main method is called there actually is no instance yet of the Main class (in this case the MyThread class).

So I've rewritten the example to make it more clear as to what happens:

public class Main
{
    public static void main(String [] args)
    {
        // main gets called when starting your program (which is a thread created by the JVM for free)

        MyThread t = new MyThread();  // You create a new Thread here
        t.start();   // You start the thread
        System.out.print("one. ");  // In the mean time, the main thread keeps running here
        System.out.print("two. ");
    }

    public static class MyThread extends Thread {
        @Override
        public void run()
        {
            System.out.print("Thread ");
        }
    }
}
john16384
  • 7,800
  • 2
  • 30
  • 44