Java - Thread - doubt

Discussion in 'Programming' started by builtSEO, Oct 17, 2011.

  1. #1
    I have a java thread program .

    
    import java.util.*;
    import java.lang.*;
    
    
    class A extends Thread
    {
    public void run()
    {
    for(int i=1;i<=5;i++)
    {System.out.println("\n error thread A:i="+i);
    }
    System.out.println("exit from A");
    }
    }
    
    class B extends Thread
    {
    public void run()
    {
    for(int j=1;j<=5;j++)
    {
    System.out.println("\n error thread B:j="+j);
    }
    System.out.println("exit from B");
    }
    }
    
    
    class C extends Thread
    {
    public void run(){
    for(int k=1;k<=5;k++)
    {System.out.println("\n error thread C:k="+k);}
    System.out.println("exit from C");}
    }
    class ThreadTest{
    public static void main(String args[])
    {
    new A().start();
    new B().start();
    new C().start();
    }
    }
     
    
    
    Code (markup):
    The output should be from the thread A, B, C. But it shows B, A , C

    After executing this:

    
    error thread B:j=1
    
     error thread B:j=2
    
     error thread B:j=3
    
     error thread B:j=4
    
     error thread B:j=5
    exit from B
    
     error thread A:i=1
    
     error thread A:i=2
    
     error thread A:i=3
    
     error thread A:i=4
    
     error thread A:i=5
    exit from A
    
     error thread C:k=1
    
     error thread C:k=2
    
     error thread C:k=3
    
     error thread C:k=4
    
     error thread C:k=5
    exit from C
    
    
    
    Code (markup):
    Why is that?
     
    builtSEO, Oct 17, 2011 IP