4

Practicing multi thread java examples ,here i am creating thread A in class A and thread B in class B .Now starting those two threads by creating objects .here i am placing the code

  package com.sri.thread;

class A extends Thread
{
    public void run()
    {
        System.out.println("Thread A");
        for(int i=1;i<=5;i++)
        {
            System.out.println("From thread A i = " + i);
        }
        System.out.println("Exit from A");

    }
}
class B extends Thread
{
    public void run()
    {
        System.out.println("Thread B");
        for(int i=1;i<=5;i++)
        {
            System.out.println("From thread B i = " + i);
        }
        System.out.println("Exit from B");
    }
}
public class Thread_Class
{
    public static void main(String[] args)
    {
        new A().start();    //creating A class thread object and calling run method
        new B().start();    //creating B class thread object and calling run method
        System.out.println("End of main thread");
    }
//- See more at: http://www.java2all.com/1/1/17/95/Technology/CORE-JAVA/Multithreading/Creating-Thread#sthash.mKjq1tCb.dpuf

}

i didn't understand the flow of execution ,tried by debugging but didn't get it.how the flow of execution is .Here i am placing the out put which confusing me.

    Thread A
Thread B
End of main thread
From thread B i = 1
From thread B i = 2
From thread B i = 3
From thread B i = 4
From thread B i = 5
Exit from B
From thread A i = 1
From thread A i = 2
From thread A i = 3
From thread A i = 4
From thread A i = 5
Exit from A

Why does the loop in thread B finish before the loop in thread A is entered?

user
  • 6,897
  • 8
  • 43
  • 79

3 Answers3

5

Unless you have multiple processors, the threads are sharing a processor on a time slice basis. The total time to execute one of your threads may be less than the time slice, so whichever thread gets dispatched first will complete before the other runs at all.

Try adding a short Thread.sleep call in the run methods.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • 1
    This. Each thread as the code is currently written runs an *extremely* tight loop; it's highly likely that the scheduler gives each thread enough time to run to completion. Also like [Kevin Bowersox said](http://stackoverflow.com/a/18163080/486504), there's no guaranteed execution order in the first place. It depends on the frobnicator state and the current neutrino count in the main deflector array. – user Aug 10 '13 at 14:57
4

There really is no guaranteed order of execution when executing multiple threads. The threads are independent of each other.

The link in the source code explains it:

Here you can see that both outputs are different though our program code is same. It happens in thread program because they are running concurrently on their own. Threads are running independently of one another and each executes whenever it has a chance.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • TQ Kevin ,is there any way t control the flow means first A should execute the thread B should execute? –  Aug 10 '13 at 15:03
  • 1
    @Prabha To do that you really don't need threads, but I take it this is a learning experiences so see this post: http://stackoverflow.com/questions/4592716/multithreading-question/4593265#4593265 – Kevin Bowersox Aug 10 '13 at 15:10
  • 1
    @Prabha It looks like `join` will work also: http://javarevisited.blogspot.com/2013/02/how-to-join-multiple-threads-in-java-example-tutorial.html – Kevin Bowersox Aug 10 '13 at 15:13
1

The threads are executing at the same time. One isn't executing inside the other. Each thread gets CPU time, does some work, and then waits while the CPUs are busy with other things. It could run differently each time depending on what else is happening on the computer.

If you expected the threads' output messages to be interlaced, they will be, to an extent, if you increase your loops to several thousands of iterations. Right now the code finishes so quickly it's hard to see that they really are running in parallel.

Tony Ennis
  • 12,000
  • 7
  • 52
  • 73