The most effective expectation is LockSupport.park/unpark , which does not require dirty (direct) use of Unsafe and does not pay for re-synchronizing the local memory cache of your stream.
This moment is important; the less work you do, the more efficient. Without synchronizing anything, you do not pay to check the thread in the main memory for updates from other threads.
In most cases, this is NOT what you want. In most cases, you want your thread to see all the updates that have occurred so far, so you must use Object.wait () and .notify (), since you must synchronize the memory state in order to use them.
LockSupport allows you to safely park a stream for a given time, and until no other stream tries to cancel your parking, it will wait a long time (excluding false awakenings). If you need to wait a certain time, you need to check the deadline again and return to park () until that time has expired.
You can use it to effectively "sleep", without the need for another thread to wake you up through LockSupport.parkNanos or .parkUntil (for millis; both methods just call Unsafe for you).
If you want other threads to wake you up, chances are good that you need memory synchronization and you should not use parking (unless you need to carefully organize variable fields without race conditions).
Good luck and happy coding!
source share