Here is how your program runs, with timestamps:
0000 StartMain
0000 Startl
3000 EndMain
6000 End1
6000 Start2
6000 End2
Why (timestamps in brackets)?
- [0000] main launches Thread1, which acquires a lock and sleeps for 6 seconds
- [1000] main launches Thread2, which can't acquire the lock held by Thread1 for 6 seconds
- [2000] main interrupts Thread2 setting its interrupted flag to true, but Thread2 is waiting for a lock and does not do anything about it
- [3000] main ends
- [6000] Thread1 finishes sleeping and releases the lock
- [6000] Thread2 can acquire it and starts to sleep (its interrupted flag is still on)
- [6000] sleep detects that Thread2 has been interrupted and throws an exception immediately
- [6000] Thread2 finishes, allowing the JVM to exit