
Everytime when i run a multithread program it gives different output: my means of executing the threads in various manners here i have one, two , three threads. At one time it runs one, two , three and on other it runs two, three, one and so on why this happens? can anyone help me thx in advance friends. Happy new year!!!!!
class Newthread3 implements Runnable{
Thread t;
String name;
Newthread3(String threadname){
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: "+t);
t.start();
}
public void run(){
try{
t.sleep(1000);
for(int i=5;i>0;i--){
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println("Interrupted");
}
System.out.println(name+" exiting");
}
}
class Demojoin{
public static void main(String a[]){
Newthread3 ob1=new Newthread3("One");
Newthread3 ob2=new Newthread3("Two");
Newthread3 ob3=new Newthread3("Three");
System.out.println("Thread One is alive: "+ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ob3.t.isAlive());
try{
System.out.println("Wait for other threads to join");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e){
System.out.println("Interrupted");
}
System.out.println("Thread One is alive: "+ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ob3.t.isAlive());
System.out.println("Main Thread Exiting");
}
}