Hackerss.com

hackerss
hackerss

Posted on

How to stop a thread in Java

How to stop a thread - Java:


/**
 * This class is used to stop a thread with javadoc.
 * @author 
 * @version 1.0
 * @since   2017-05-24
 */
public class StopThread {
   /**
   * This method is used to stop a thread.
   * @param args the command line arguments
   */   
   public static void main(String[] args) {
      //Create a new thread
      Thread thread = new Thread(new Runnable() {
         @Override
         public void run() {
            //Print the message
            System.out.println("Hello World");
         }
      });
      //Start the thread
      thread.start();
      //Stop the thread after 5 seconds
      try {
         Thread.sleep(5000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
      //Stop the thread
      thread.stop();
   }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)