fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) throws InterruptedException {
  11.  
  12. Runnable toRun = new Runnable() {
  13. @Override
  14. public void run() {
  15. long millis = System.currentTimeMillis();
  16. try {
  17. Thread.sleep(3000);
  18. } catch (InterruptedException e) {
  19. boolean isi = Thread.currentThread().isInterrupted();
  20. e.printStackTrace();
  21. System.out.printf("Catch -> am interrupted %b\n", isi);
  22. Thread.currentThread().interrupt();
  23. } finally {
  24. System.out.printf("Finally1 -> I am interrupted %b in %d millis\n", Thread.currentThread().isInterrupted(), System.currentTimeMillis() - millis);
  25. System.out.printf("Finally2 -> I am interrupted %b\n", Thread.currentThread().isInterrupted());
  26. }
  27. }
  28.  
  29. };
  30.  
  31. Thread t = new Thread(toRun, "TestThread");
  32.  
  33. t.setDaemon(true);
  34. t.start();
  35.  
  36. Thread.sleep(2000);
  37.  
  38. t.interrupt();
  39.  
  40. Thread.sleep(2000);
  41. System.out.println("Done");
  42.  
  43. }
  44. }
Success #stdin #stdout #stderr 0.07s 380544KB
stdin
Standard input is empty
stdout
Catch -> am interrupted false
Finally1 -> I am interrupted true in 2006 millis
Finally2 -> I am interrupted true
Done
stderr
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at Ideone$1.run(Main.java:17)
	at java.lang.Thread.run(Thread.java:722)