生活随笔
收集整理的这篇文章主要介绍了
JUC并发编程八 并发架构--park,unpark
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
wait,notify和notifyAll必须配合 Object Monitor一起使用.而park,unpark不用.park 和 unpark是以线程为单位阻塞和唤醒线程.而notify只能随机唤醒一个阻塞线程,而notifyAll唤醒所有线程,就不那么精确.park和unpark,可以先unpark. 但wait/notify,则不能先notify.
import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.LockSupport;@Slf4j(topic
= "c.TestParkUnPark")
public class TestParkUnPark {public static void main(String[] args
){Thread t1
= new Thread(()->{log
.debug("start...");try {Thread.sleep(5000);} catch (InterruptedException e
) {e
.printStackTrace();}log
.debug("park...");LockSupport.park();log
.debug("resume");},"t1");t1
.start();try {Thread.sleep(1000);} catch (InterruptedException e
) {e
.printStackTrace();}log
.debug("unpark...");LockSupport.unpark(t1
);}
}
总结
以上是生活随笔为你收集整理的JUC并发编程八 并发架构--park,unpark的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。