public
class
InterruptBeforeSleep {
public
static
void
main(String[] args) {
Runnable task =
new
Runnable() {
public
void
run() {
System.out.println(
"Running task"
);
double
dummy =
0.11111
;
for
(
double
i =
2.0
; i <
100000000.0
; i++) {
dummy *= (i / (i-
1
));
}
System.out.println(
"Result is: "
+ dummy);
System.out.println(
"About to sleep"
);
try
{
Thread.sleep(SECONDS.toMillis(
20
));
}
catch
(InterruptedException e) {
System.out.println(
"I'm interrupted"
);
System.out.println(
"I'm interrupted? "
+ Thread.currentThread().isInterrupted());
}
}
};
Thread t =
new
Thread(task);
t.start();
t.interrupt();
System.out.println(
"Main interrupted the task thread"
);
}
}