Niranjan Sonachalam
Niranjan Sonachalam

Reputation: 1625

Getting different thread order even time I run my program

can someone tell me the order in which a thread starts to execute?. I have written the following code

class NewThread implements Runnable {
    Thread t;
    NewThread() {
        //creating a second thread.
        t=new Thread(this,"Demo Thread");
        System.out.println("Child Thread:"+t);
        t.start();
    }

    public void run() {
        try {
            for(int i=0;i<5;i++) {
                System.out.println("Child Thread:"+i);
                Thread.sleep(3000);
            }
        } catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        System.out.println("Exiting Child Thread");
    }
}

and this

public class ThreadDemo {
    public static void main(String args[]) {
        new NewThread();
        try {
            for(int i=0;i<5;i++) {
                System.out.println("Main Thread:"+i);
                Thread.sleep(3000);
            }
        } catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
        System.out.println("Exiting Main Thread");
    }
}

when I execute this code, I get many different sets of output.

Child Thread:Thread[Demo Thread,5,main]
Main Thread:0
Child Thread:0
Child Thread:1
Main Thread:1
Main Thread:2
Child Thread:2
Main Thread:3
Child Thread:3
Main Thread:4
Child Thread:4
Exiting Main Thread
Exiting Child Thread

another one,

Child Thread:Thread[Demo Thread,5,main]
Main Thread:0
Child Thread:0
Child Thread:1
Main Thread:1
Child Thread:2
Main Thread:2
Child Thread:3
Main Thread:3
Child Thread:4
Main Thread:4
Exiting Child Thread
Exiting Main Thread

Why is this occurring? Will the thread order never be same?? and it would be nice, if someone can give me pointers to basics of threading and examples. P.S: I am new to threads and this is my first thread program. Thanks in advance.

Upvotes: 2

Views: 263

Answers (3)

Gray
Gray

Reputation: 116878

when I execute this code, I get many different sets of output.

This is expected. The order is not defined and is subject to race conditions as the threads start running and are subjected to thread scheduling by the OS.

The whole reason why we write multi-threaded applications is that the threads are asynchronous and run in separate processors for speed reasons. To guarantee specific output order, you could synchronize between the threads with locks and the like but you would then lose performance and the whole reason for forking threads would be diminished.

Upvotes: 8

srini.venigalla
srini.venigalla

Reputation: 5145

There is no implicit order. If you must have an order, use the ExecutorService with a que depth of 1.

Upvotes: 0

BlackVegetable
BlackVegetable

Reputation: 13044

You have encountered a race condition.

You have stumbled across one of the complexities of multi-threading. If you have code that MUST be processed in a particular order you must "lock" that code and/or declare variables within it "volatile". Run a google search for "deadlocks" and "race conditions".

Upvotes: 0

Related Questions