fork 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.currentThread().interrupt();
  18. throw new InterruptedException();
  19. } catch (InterruptedException e) {
  20. boolean isi = Thread.currentThread().isInterrupted();
  21. e.printStackTrace();
  22. System.out.printf("Catch -> am interrupted %b\n", isi);
  23. Thread.currentThread().interrupt();
  24. } finally {
  25. System.out.printf("Finally1 -> I am interrupted %b in %d millis\n", Thread.currentThread().isInterrupted(), System.currentTimeMillis() - millis);
  26. System.out.printf("Finally2 -> I am interrupted %b\n", Thread.currentThread().isInterrupted());
  27. }
  28. }
  29.  
  30. };
  31.  
  32. Thread t = new Thread(toRun, "TestThread");
  33.  
  34. t.setDaemon(true);
  35. t.start();
  36.  
  37. Thread.sleep(2000);
  38.  
  39. t.interrupt();
  40.  
  41. Thread.sleep(2000);
  42. System.out.println("Done");
  43.  
  44. }
  45. }
Success #stdin #stdout #stderr 0.08s 380480KB
stdin
Standard input is empty
stdout
Catch -> am interrupted true
Finally1 -> I am interrupted true in 6 millis
Finally2 -> I am interrupted true
Done
stderr
java.lang.InterruptedException
	at Ideone$1.run(Main.java:18)
	at java.lang.Thread.run(Thread.java:722)