-3

I am trying to learn about the join() method. In my code when I use DEBUG mode it first calls the run() method, but when I run it my output differs. When we try to start a thread using start() method, the run() method will be called internally and it gets executed. But in my output it differs. Can anyone suggest me a solution?

class JoinExample extends Thread{  
    public void run(){  
        System.out.println("CurrentThread:"+
                           Thread.currentThread().getName());
        System.out.println("Is Alive?"+ 
                           Thread.currentThread().isAlive());

    }  
    public static void main(String args[]){  

        JoinExample t1=new JoinExample();  
        JoinExample t2=new JoinExample();  
        JoinExample t3=new JoinExample();

        t1.start();  

        System.out.println("Is Alive?" + 
                           t1.isAlive());
        t2.start();  
        t3.start();  
    }  
} 

My output when I use DEBUG mode:

Current Thread:Thread-0
Is Alive?true
Is Alive?false
Current Thread:Thread-1
Is Alive?true
Current Thread:Thread-2
Is Alive?true

My output when I run my code:

Is Alive?true
Current Thread:Thread-1
Is Alive?true
Current Thread:Thread-0
Current Thread:Thread-2
Is Alive?true
Is Alive?true
khelwood
  • 55,782
  • 14
  • 81
  • 108
Heyyou
  • 151
  • 1
  • 13
  • This question is poorly explain, also you should show the output of your code. – Miguel Garnacho Vélez Jan 25 '19 at 11:33
  • I have edited the question with output now. @ Miguel Garnacho Vélez – Heyyou Jan 25 '19 at 11:34
  • What did you expect? A thread is not executed sequentially with the other threads. calling `t1.start()` only tells the thread to start. Your current thread will continue execution and `t1` will execute in parallel. The output will always be random, its determined by the OS scheduler. – Zabuzard Jan 25 '19 at 11:34
  • Start *is* called before the `println`. It's just that the thread scheduler doesn't actually start the work immediately. – Andy Turner Jan 25 '19 at 11:34
  • When you start a thread, it's undefined what happens first afterwards. Maybe your main thread prints first, maybe the thread... – Robert Kock Jan 25 '19 at 11:35

2 Answers2

1

The executing order of any thread is not determinist, that means that it will always vary from execution to execution, and it is impossible to know the order.

The DEBUG mode does not change anything.

If you want them to be executed in order, use the join method this way:

 t1.start();  
 t1.join();
 t2.start();  
 t2.join();
 t3.start();
 t3.join(); 

This way the code will wait for thread number 1 to finish, and then it will start thead number 2, and so on.

1

This is expected behaviour, nothing to do with DEBUG or RUN.

If you'll run it many times, you will get different results.

When you start() a Thread it submitted to the Thread Scheduler.

Thread Scheduler is a part of the JVM that decides which thread should run. There is no guarantee that which Thread will be chosen for execution. The Thread Scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

You can join all the threads, so one will wait for another, but it doesn't make sense.

All the point of multithreading is parallel/concurrent execution.

J-Alex
  • 6,881
  • 10
  • 46
  • 64