Home > Back-end >  Park after executing interrupted () () failure
Park after executing interrupted () () failure

Time:10-03

To perform first interrupted () method can get the expected results, thread interrupt tag is empty, park () can normal blocking threads
 private static void test4 () {
Thread the t1=new Thread (() - & gt; {
for (int i=0; I & lt; 5; I++) {
The debug (" park... ");
LockSupport. Park ();
Log. The debug (" interrupt status: {} ", Thread interrupted ());
Log. The debug (" interrupt status: {} ", Thread currentThread (). The isInterrupted ());
}
});
T1. Start ();


Sleep (1);
T1 interrupt ();
}

The results
 14:59:59. 983 c.T estInterrupt [Thread - 0] - park... 
15:00:00. 982 c.T estInterrupt [Thread - 0] - interrupt status: true
15:00:00. 983 c.T estInterrupt [Thread - 0] - interrupt status: false
15:00:00. 983 c.T estInterrupt [Thread - 0] - park...

If it is to run the isInterrupted () method, then the park () until the next interrupted () will block the thread when
 private static void test4 () {
Thread the t1=new Thread (() - & gt; {
for (int i=0; I & lt; 5; I++) {
The debug (" park... ");
LockSupport. Park ();
Log. The debug (" interrupt status: {} ", Thread currentThread (). The isInterrupted ());
Log. The debug (" interrupt status: {} ", Thread interrupted ());
}
});
T1. Start ();


Sleep (1);
T1 interrupt ();
}


The results
 15:05:34. 855 c.T estInterrupt [Thread - 0] - park... 
15:05:35. 853 c.T estInterrupt [Thread - 0] - interrupt status: true
15:05:35. 854 c.T estInterrupt [Thread - 0] - interrupt status: true
15:05:35. 855 c.T estInterrupt [Thread - 0] - park...
15:05:35. 855 c.T estInterrupt [Thread - 0] - interrupt status: false
15:05:35. 855 c.T estInterrupt [Thread - 0] - interrupt status: false
15:05:35. 855 c.T estInterrupt [Thread - 0] - park...

  • Related