Skip to content

Instantly share code, notes, and snippets.

@kasundharmadasa
Created December 5, 2018 12:19
Show Gist options
  • Save kasundharmadasa/76c6939c23a926891615f7721e07a9e1 to your computer and use it in GitHub Desktop.
Save kasundharmadasa/76c6939c23a926891615f7721e07a9e1 to your computer and use it in GitHub Desktop.
public class StopThread {
private static boolean 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);
stopRequested = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment