+1 vote
104 views
in Java Interview Question by
Please see below code and find error or bugs :

public class Thread1 {


        private static  boolean interruptedTask=false;

        private static class MyClass extends Thread{

            public void run(){

                while(!interruptedTask){

                //Do some time consuming task for more than 5 secs

                }

            }

        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Thread task=new MyClass();
            task.start();

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            interruptedTask=true;
            try {
                task.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

    }

Your answer

Thanks for contributing your answer!
Please be sure to answer the question. Provide details with examples and share your research!
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated