NeverGiveUp161
NeverGiveUp161

Reputation: 850

Thread run() method execution different on Run and Debug in eclipse

For the below program different outputs are coming when running and debugging on eclipse.

 public class MyClass implements Runnable {
        public static void main (String[] args) throws Exception {
            Thread t = new Thread(new MyClass());
            t.start();
            System.out.print("Started");
            t.join();
            System.out.print("Complete");
        }
        public void run() {
            for (int i = 0; i < 4; i++) {
                System.out.print(i);
            }
        }
}

When running this as java application the OUTPUT is

Started0123Complete

When checking in Debug Mode OUTPUT is

0123StartedComplete

Can someone help on this? is it because of two threads? main thread and the thread which starts with t.start().If yes then why main thread execution is taking more priority to complete first?

Thanks

Upvotes: 2

Views: 4238

Answers (5)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279950

It's coincidence. You can't control the thread scheduler.

However, in a debug environment, the debugger plugs into your code to inspect values and execution. This inspection increases the amount of work done per thread time slice.

For example, in a regular run, the main thread may only need 1 time slice to create the new thread object, start it, and print the Started message. Then a context switch would occur and the second thread would get a chance to work. In a debug run, the main thread would only have enough time to create the new thread object and start the corresponding thread. Then the context switch would occur and the other thread would do its thing.

Upvotes: 3

Manoja Brahma
Manoja Brahma

Reputation: 29

In both (Run / Debug) the mode output will be

Started0123Complete

You are seeing result 0123StartedComplete, because you must be having debug point inside main thread code, that’s why.

If you want to understand better, then move the run() method to inside MyClass and put debugger point inside run method now you will see it is printing as Started0123Complete.

Internally, Main Thread is creating subthread “t” and when t.start() is being called it is waiting to capture the monitor for thread execution (i.e. run()) and when you are adding debug statement in main thread, sub-thread is entering into monitor and run method is executing and once it completed, main thread is starting again.

Upvotes: 0

Cristian Sevescu
Cristian Sevescu

Reputation: 1489

It is not related to being in debug mode or not. There is no guarantee of when this gets executed

System.out.print("Started"); //this is in the main thread

compared to

for (int i = 0; i < 4; i++) { // this is in the started thread
            System.out.print(i);
        }

It can even be

01Started23Complete

Upvotes: 1

Pandiri
Pandiri

Reputation: 329

Yes, Its because of 2 threads But the output is unpredictable. No, the main thread doesn't get high priority because its MAIN. you can't say any particular interleaved execution in multithreads.

In Debug, my guess is you put a break point for main thread so it waits and in the mean time the other thread might have executed.

Upvotes: 0

Giovanni Botta
Giovanni Botta

Reputation: 9816

The order in which the string "Started" and the integers are printed out is undefined by definition. After you call start, there is no guarantee that the code in the run method will be executed before or after any other statement that appears before the call to join. This is the nature of multithreaded applications.

The fact that you see a certain output in debug mode vs run mode is probably purely accidental and might change if you run your code many times or on different platforms/versions of the JVM. If you need deterministic order in this case, the only way you can achieve it is to print a string before calling start or introduce some other sort of semaphore to force the thread to wait for the main thread or viceversa.

Upvotes: 6

Related Questions