Skip to content

Instantly share code, notes, and snippets.

@kasundharmadasa
Created December 5, 2018 14:46
Show Gist options
  • Save kasundharmadasa/9a327571f40a0ee500f6dfa6e31acccc to your computer and use it in GitHub Desktop.
Save kasundharmadasa/9a327571f40a0ee500f6dfa6e31acccc to your computer and use it in GitHub Desktop.
public class StopThread {
private static boolean stopRequested;
private static synchronized void requestStop() {
stopRequested = true;
}
private static synchronized boolean stopRequested() {
return stopRequested;
}
public static void main(String[] args)
throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
public void run() {
int i = 0;
while (!stopRequested())
i++;
}
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
requestStop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment