On the problems of Unsafe.park () and Thread.interrupt ()

  public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {
    Unsafe unsafe = UnsafeUtil.getUnsafe();

    Thread currThread = Thread.currentThread();
    new Thread(()->{
      try {
        Thread.sleep(3000);
        currThread.interrupt();
        //unsafe.unpark(currThread);
      } catch (Exception e) {
        System.out.println(e);
      }
    }).start();

    unsafe.park(false, 0);
    //currThread.sleep(5000);

    System.out.println("SUCCESS!!!");
  }

result: the program will not report an error and print success after three seconds

if you replace unsafe.park (false, 0); with currThread.sleep (5000), a thread interrupt exception will be reported.

I know that Thread.interrupt will throw an InterruptedException exception on a blocked thread.

but isn"t park blocking? In fact, I haven"t quite understood the difference between the Object.wait () and Unsafe.park () methods.

also ask the great gods to help explain.

Mar.29,2021
The pack operation of

1.unsafe can be returned for no reason.
2.unsafe.pack after suspending the current thread, InterruptedException will not be thrown if the thread is interrupted, but if you look at the currThread.isInterrupted (), of the current thread, you will find that it is indeed interrupted.
3. Under normal circumstances, when a thread wait is interrupted, an exception will be thrown, and the interrupt flag will be reset after throwing an exception, that is, after you catch InterruptedException, you will return false when you call currThread.isInterrupted (), but if the thread is interrupted when doing work that has nothing to do with thread cooperation (that is, no wait, and no sleep, join), will not throw an exception, and call currThread.isInterruped to see that the interrupt has indeed occurred. The superficial difference between the
4.Object.wait () and Unsafe.park () methods can be seen in 3, but the deeper difference is hard to say. Unsafe is a method that is not recommended in the first place.


look at the source code. The park method of
UNSAFE is as follows. It is not that you do not wait, but that the wait of prak will not throw an InterruptedException exception.

public static native void sleep(long millis) throws InterruptedException;

interrupts are not thread states, but just throw InterruptedException to let you know that if you need special processing logic, you can add it, and forget it. Park just didn't tell you. Sleep told you.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e44f17-44af6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e44f17-44af6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?