Home Answers Viewqa Java-Beginners Threads(suspend(),resume()) run time abnormal behaviour

 
 


Tushar Arora
Threads(suspend(),resume()) run time abnormal behaviour
0 Answer(s)      3 months and 24 days ago
Posted in : Java Beginners

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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.