#LockSupport wait park sleep 范例
实际项目用的少,架不住面试用啊,concurrenthashmap 里有。
全文讲解 : https://pdai.tech/md/java/thread/java-thread-x-lock-LockSupport.html
摘要:
wait notify 先后顺序不能乱, 先执行了notify, wait()将一直阻塞,因为后续将没有其它notify()唤醒它。
object.wait() 会释放锁,Object.wait()方法需要在synchronized块中执行;
LockSupport.park() 不会释放锁
LockSupport.park()方法可以被另一个线程调用LockSupport.unpark()方法唤醒;
park unpark 先后顺序不重要。 先unpark,在执行park也可以过
Makes available the permit for the given thread, if it was not already available. If the thread was blocked on park then it will unblock. Otherwise, its next call to park is guaranteed not to block. This operation is not guaranteed to have any effect at all if the given thread has not been started.
从功能上来说,Thread.sleep()和LockSupport.park()方法类似,都是阻塞当前线程的执行,且都不会释放当前线程占有的锁资源;
sleep 也不会释放锁
Thread.sleep()没法从外部唤醒,只能自己醒过来;
在调用 wait 方法之后,线程会变为 WATING 状态,而调用 sleep 方法之后,线程会变为 TIMED_WAITING 状态。