1
class mythread implements Runnable {

    Thread t1;
    String name = "";

    public mythread(String thname) {
         name = thname;
         t1= new Thread(this, name);
         System.out.println(t1);
         t1.start();
         System.out.println(t1.getName());
     }
     @Override
     public void run() {
         for (int i=5;i>0;i--){
             try {
                System.out.println(Thread.currentThread());
                 System.out.println("child Thread" + i);
                 Thread.sleep(2000);
             }  catch(InterruptedException e){
                System.out.println("Child Thread Interrupted");
             }
         }
     }
}

public class Mainthread {
   public static void main(String[] args) {
        mythread m1 = new mythread("Rohan");
        mythread m2 = new  mythread("Jain");

        try {
            for(int i=5;i>0;i--){
                System.out.println("Main Thread" + i);
                 Thread.sleep(2000);
            }
        } catch(InterruptedException e){
            System.out.println("Main Thread Interrupted");
        }
    }
}

The output is:

Thread[Rohan,5,main]
Rohan
Thread[Jain,5,main]
Thread[Rohan,5,main]
child Thread5
Jain
Main Thread5
Thread[Jain,5,main]
child Thread5
Main Thread4
Thread[Rohan,5,main]
child Thread4
Thread[Jain,5,main]
child Thread4
Main Thread3
Thread[Rohan,5,main]
child Thread3
Thread[Jain,5,main]
child Thread3
Main Thread2
Thread[Jain,5,main]
Thread[Rohan,5,main]
child Thread2
child Thread2
Thread[Rohan,5,main]
child Thread1
Thread[Jain,5,main]
child Thread1
Main Thread1

but the output i want is like first it should print the 5 in thread "rohan" then 5 in thread in "jain" then 5 in thread "main" and so on...please help..!!!!!!

Gray
  • 115,027
  • 24
  • 293
  • 354
roanjain
  • 1,252
  • 4
  • 14
  • 32

3 Answers3

8

These sort of questions really confuse me. The whole point of threads is that they run asynchronously in parallel so we get better performance. The order that threads run cannot be predicted due to hardware, race-conditions, time-slicing randomness, and other factors. Anyone who is asking about specific order of output in a threaded program should not be using threads at all.

Here are similar answers to the same question:

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    +1. It's like asking how to use a cabbage to write a novel. The question itself is senseless (unless you're in the python chat room). – roippi Jul 23 '13 at 16:52
2

You may want to use locks on a single object to control the sequencing. Please refer Java Thread Locks tutorial for further details.

Gray
  • 115,027
  • 24
  • 293
  • 354
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

Threads are given processor time or scheduled at the OS level. So your program doesn't have direct control over the times when instructions are executed across its own threads. So there is now guarantee of order. There are a handful of OS specific variables that go into scheduling.

If you want to guarantee order across threads than your threads need to communicate that. There are a handful of ways to do this. Most commonly programmers use a mutex; which is also called a lock.

DubiousPusher
  • 1,132
  • 2
  • 8
  • 19