Advertisement
NLinker

Manually run and then stop worker thread

Aug 4th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.86 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) throws InterruptedException {
  4.         final Thread w = new Thread() {
  5.             @Override
  6.             public void run() {
  7.                 boolean toExit = false;
  8.                 while (!isInterrupted() && !toExit) {
  9.                     try {
  10.                         Thread.sleep(500);
  11.                         System.out.println("Thread.sleep(500);");
  12.                     } catch (InterruptedException e) {
  13.                         System.err.println(e.getLocalizedMessage());
  14.                         toExit = true;
  15.                     }
  16.                 }
  17.             }
  18.         };
  19.         // w.setDaemon(true);
  20.         w.start();
  21.  
  22.         long date = new Date().getTime();
  23.         int i = 0;
  24.         Thread.sleep(5000);
  25.         w.interrupt();
  26.         System.err.println("finish all");
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement