Friday, January 10, 2014

Another Example of Threding

class CreateThreadDemo
{
    public static void main(String surat[])
    {
        new NewThread();
        try
        {
            for(int k=10;k>6;k--)
            {
                System.out.println("Parent Thread : "+k);
                Thread.sleep(2000);
            }
        }
        catch(InterruptedException e)
        {
            System.out.println("Parent Interrupted.");
        }
        System.out.println("Existing parent");
    }
}
class NewThread implements Runnable
{
    Thread T;
    NewThread()
    {
        T=new Thread(this,"Creating thread");
        System.out.println("Thread of Child : "+T);
        T.start();
    }
    public void run()
    {
        try
        {
            for(int j=10;j>6;j--)
            {
                System.out.println("Child Thread : "+j);
                Thread.sleep(2000);
            }
        }
        catch(InterruptedException e)
        {
            System.out.println("Child Interrupted.");
        }
        System.out.println("Exiting Child.");
    }
}

Output:

No comments:

Post a Comment