0

Here is my code

public class ThreadLearn{
private static class Check implements Runnable{
    public void run(){

        System.out.printf("\nInCheck %s", Thread.currentThread().getName());
    }
}

public static void main(String[] args){

   Thread t;
   int i=0;
   while(i<2){
      t = new Thread(new GetData());
      t.start();
      System.out.printf("\n%s: ", t.getName());
      i++;
   }

}

}

The output is:

InRun Thread-0
Thread-0:
Thread-1:
InRun Thread-1

I have two questions:

  1. Should not the output be

    InRun Thread-0

    Thread-0:

    InRun Thread-1

    Thread-1:

  2. Once the t.start() is done, will it wait for the run() to be executed and then create the second thread? what if i want to do the following

    while(required)

    new Thread().start();

and in my run method i can have all the stuff that i want the threads to accomplish in parallel so that it doesnt wait for one thread to complete the run method and then create the second thread.

Thanks

Kraken
  • 23,393
  • 37
  • 102
  • 162

3 Answers3

6

Should not the output be ...

No. Threads run independently of each other. The JVM and operating system decide when which thread is running. In general, the order in which two threads are executed is unpredictable. It could very well happen that when you run the program again, the result is different.

Once the t.start() is done, will it wait for the run() to be executed and then create the second thread?

No, it will not wait. The run() method of the new thread may start immediately, or later, depending on how threads are being scheduled.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • so it is already fulfilling my question 2 right? that the threads are craeated and they run independently without waiting for anything. I can just put the implementation in run method and they will keep executing in parallel? – Kraken Aug 24 '12 at 07:25
  • 1
    @Kraken Yes, that's the whole point of threads, that you can have multiple things running in parallel, independently of each other. – Jesper Aug 24 '12 at 07:49
  • say i created a new thread `t` and i have a function in my class `Check` or in any other class that i want this thread to run, how do i do it since `t.method()` wont work? How to do that? – Kraken Aug 24 '12 at 08:53
1

When the thread is started, it's started. So the two System.out are executed at nearly the exact same time, causing them to sometimes change order

Example:

            ---- thread-0 prints out  
Main thread |--- Prints out ----------- Prints out
                                      |Thread-1 started, prints out

As you can see, the branches will run at approximately the same time as the main code, causing them to fluctuate between which actually prints out first

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • so if i want to spawn multiple threads and let them work in parallel, i can use the same approach as above and put the implementation in the `run()` method right? – Kraken Aug 24 '12 at 07:27
0

Since you have not provided any synchronization mechanism, there is no sure way to know which thread is going to be executed first; at different runs, there maybe other order.

Random42
  • 8,989
  • 6
  • 55
  • 86