Unsafe.park vs Object.wait

后端 未结 4 722
我在风中等你
我在风中等你 2020-12-24 08:03

I have a couple of questions regarding Unsafe.park and Object.wait (and their corresponding resume methods):

  1. Which one should be use
4条回答
  •  不思量自难忘°
    2020-12-24 08:40

    If you're managing concurrency with synchronized blocks, then you would use Object.wait, notify, and notifyAll for signalling. This is the first kind of concurrency control that Java supported, and it was considered to be very easy to use at the time. It certainly was, compared to everything else that was around.

    These days, though, there are lots of classes in java.util.concurrent don't require as much specialized knowledge to work with. These are the things that should be used by average programmers these days.

    The park* and unpark methods in LockSupport are what you would use if you are writing your own lock-free algorithms and data structures. They are high-performance constructs that don't require locks to work with, and they are very well designed to make this kind of work as easy as it can be... but that is still very difficult and tricky work that is best left to experts.

提交回复
热议问题