0

Please help me figure out what when wrong in this coding.

import java.lang.Thread;

class MyThread1 extends Thread
{ public void run()
{    for(int x=1;x<=15;x++)
  {  if(x==10)
   { stop();
    }
System.out.println(x);
}
}
}
class MyMain
{ public static void main(String args[])
{ MyThread1 first_thread=new MyThread1();
first_thread.start();
}
}

the error is Note: MyClass.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

idamas
  • 1
  • 1

1 Answers1

0

The problem is that you should not call Thread.stop(). It is deprecated and dangerous.

Read Thread.stop() - deprecated to find out why stop() is deprecated. Or the javadoc.

If you want the thread to terminate itself, it can simply return from the run() method:

public void run() { 
    for (int x = 1; x <= 15; x++) {  
        if (x == 10) {
            return;
        }
    }
}

or throw an unchecked exception:

public void run() { 
    for (int x = 1; x <= 15; x++) {  
        if (x == 10) {
            throw new RuntimeException("It is 10am and your pants are on fire!");
        }
    }
}

Obviously, you should choose a more appropriate exception and message ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216