-1

Below is my Multithreading class:

public class Multithreading extends Thread{

    public void run(){
         for(int i=1;i<5;i++){  
             try{
                 Thread.sleep(500);
             }catch(InterruptedException e){
                    System.out.println(e);
                }  
          System.out.println(i);  
          }  
    }

    public static void main(String args[])
    {
        Multithreading t1 = new Multithreading();
        Multithreading t2 = new Multithreading();
        t1.start();
        t2.run();
    }
}

And, this the output what I got:

1 1 2 2 3 3 4 4

Could you please explain the output, I mean how this execution of start() and run() is working here.

A.v
  • 734
  • 5
  • 26
Rohit
  • 188
  • 2
  • 18
  • `t2.run()` - no thread is created here. `t2` is simply an object that you run `run` method on it. – Maroun Jun 23 '15 at 07:47

1 Answers1

1

We cannot predicate output order in case of Threads.

Multithreading t1 = new Multithreading();
    Multithreading t2 = new Multithreading();
    t1.start(); // Thread is executing your run() method
    t2.run(); // It is a normal execution of run() method. No Thread is here