Threads(suspend(),resume()) run time abnormal behaviour

Threads(suspend(),resume()) run time abnormal behaviour

class A implements Runnable { Thread t; A() { t=new Thread(this); t.start(); }

    public void run()
    {
        int i=0;
        while(true)
        {
            //try
            //{
                System.out.println(i++);
            //  Thread.sleep(2000);
            //}
            //catch(InterruptedException e)
            //{
            //  System.out.println(e);
            //}
        }
    }
    public static void main(String arg[])
    {
        A a1=new A();
        try
        {
            Thread.sleep(1000);
        }
        catch(InterruptedException e)
        {
            System.out.println(e);
        }
        System.out.println("suspended");
        a1.t.suspend();
        try
        {
            System.out.println("try enter");
            Thread.sleep(10000);
            System.out.println("try left");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }   
        System.out.println("resumed");  
        a1.t.resume();
    }

}

hi! when i run this code after some seconds A thread and main thread both stop working and the program hangs in between and prints nothing.But when i remove the // from the lines which i commented in run method of class A i.e. i make the commented lines as part of my program or i make A thread sleep for 2 seconds in between then my program does not hang in between and continues normally.PLEASE HELP me out!!!!!

View Answers









Related Tutorials/Questions & Answers:

Ads