Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. The order of threads execution

I try to run the example from a book(Paul Hyde, Java Thread Programming). It says that the order of threads will interchange. But I always get : 10 "Main thread" prints and 10 "New thread" ones afterwards. What is more interesting : if I will use tt.run instead of tt.start then result will be vice versa. Maybe the reason that the book is quite old and examples are base on JDK 1.2??? Code is below:

public class TwoThread extends Thread
{
    public void run()
    {
        for (int i = 0; i < 10; i++)
        {
            System.out.println("New thread");
        }
    }

    public static void main(String[] args)
    {
        TwoThread tt = new TwoThread();
        tt.start();

        for (int i = 0; i < 10; i++)
        {
            System.out.println("Main thread");
        }
    }
}
like image 838
Leo Mish Avatar asked Sep 14 '15 06:09

Leo Mish


People also ask

What order do threads run in?

Because threads run at the same time as other parts of the program, there is no way to know in which order the code will run.

Can we decide order of execution of threads?

You cannot tell the thread scheduler which order to execute threads in. If you need to ensure that a certain piece of code which is running on thread A must run before another piece of code running on thread B, you must enforce that order using locks or wait() / notify() .

Which thread will execute first in Java?

When a Java program starts up, one thread begins running immediately. This is usually called the main thread of our program because it is the one that is executed when our program begins.

How are Java threads executed?

start. Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).


2 Answers

The JVM decides when to transfer control from the main thread to the second thread. Since the main thread doesn't perform much work after starting the second thread, it makes sense the JVM lets it complete its work before trasfering control to the second thread.

When you use tt.run() instead of tt.start() you are not starting a second thread. You are executing the run() method in the main thread. Therefore you see the "New thread" outputs first.

like image 127
Eran Avatar answered Oct 13 '22 16:10

Eran


if I will use tt.run instead of tt.start then result will be vice versa.

tt.run runs in main thread only whereas tt.start spawns another thread. Hence the result is expected.

On seeing consistency:

for smaller and quick activities you may see consistent result occasioanly but that doesn't mean the order of print you are seeing is guaranteed. Increase loop iteration to higher number and see the order of print (say printing upto 10000 times)

like image 24
harsh Avatar answered Oct 13 '22 18:10

harsh