I am trying to print numbers from 1 to 10 in sequential order with two different threads, but output is not always same : <p>This is how my code looks like, What is wrong with the code?
I want to print numbers in 1, 2,3,4,....10 but the output is 2,1,3,4,6...and it changes every time or is their any better way to implement this</p>

<pre><code>class Th1 extends Thread{
    public void run (){
        try{
            for (int i=1; i&lt;=10; i+=2){
            System.out.println ("VALUE OF ODD : "+i);
            Thread.sleep (1000);
            }
        }catch (InterruptedException ie){
            System.out.println (ie);
        }
    }
};
class Th2 implements Runnable{
    public void run (){
        try{
            for (int j=2; j&lt;=10; j+=2){
                System.out.println ("VALUE OF EVEN : "+j);
                Thread.sleep (1000);
            }
        }catch (InterruptedException ie){
            System.out.println (ie);
        }
    }
};
class ThDemo6{
    public static void main (String [] args)    {
        Th1 t1=new Th1 ();// object of Thread class
        Th2 t2=new Th2 ();// object of Runnable class
        Thread t=new Thread (t2);// Runnable is converted into Thread object
        System.out.println ("BEFORE START T1 IS : "+t1.isAlive ());
        System.out.println ("BEFORE START T2 IS : "+t.isAlive ());
        t1.start ();
        t.start ();
        System.out.println ("AFTER START T1 IS : "+t1.isAlive ());
        System.out.println ("AFTER START T2 IS : "+t.isAlive ());
        try        {
            t1.join ();// to make thread to join together for getting performance
            t.join ();
        }        catch (InterruptedException ie)        {
            System.out.println (ie);
        }
        System.out.println ("AFTER JOINING T1 IS : "+t1.isAlive ());
        System.out.println ("AFTER JOINING T2 IS : "+t.isAlive ());
    }
}
</code></pre>
