2018年最新Python爬虫+书籍分享

Every thread can be in one of a number of different states: Running means that the thread is actually running on a CPU, Runnable means that nothing is preventing the thread from running except, maybe the availability of a CPU for it to run on. All of the other states can be lumped into a category called blocked. A blocked thread is a thread that is waiting for something to happen before it can become runnable.

线程可以划分为三种状态:运行状态,表示正在运行;可运行状态,表示只要cpu资源足够即可立即执行;阻塞状态,除了前面两种状态的状态,它需要等待发生一些事情使得它变为可运行状态。

When your program calls Thread.yield(), it's saying to the operating system, "I still have work to do, but it might not be as important as the work that some other thread is doing. Please send me to the back of the run queue right now." If there is an available CPU for the thread to run on though, then it effectively will just keep running (i.e., the yield() call will immediately return).

yield,表示线程尝试放弃暂用cpu资源提供其它更重要的线程优先使用,不过如果cpu资源充足,yield方法也可继续自行立即结束并返回;所以说只是一个hint,并不表示调用yield的方法一定会被立即阻塞;

When your program calls foobar.wait() on the other hand, it's saying to the operating system, "Block me until some other thread calls foobar.notify().

当线程调用foobar.wait()时,线程立马进入阻塞状态,它需要另一个线程来唤醒它,使它进入可执行状态;

 

说点有意思的,对于如下两种结构都可以实现线程之间的协作,但是wait有个明显的局限,编码上要求先拿到锁。

while(满足条件){

     obj.yield();

}

while(满足条件){

     obj.wait();

}

还有笔者看hikari (版本3.2.1-SNAPSHOT)源码中并没有使用过wait这种结构,ConcurrentBag类中却三次使用yield(),其中两次是上面yield结构,从这里笔者推断yield结构的性能应该会更好。
--------------------- 
作者:xingshen100 
来源:CSDN 
原文:https://blog.csdn.net/xingshen100/article/details/85022126 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值