python阻塞队列,

  python阻塞队列,

  今天,面试官问我消息队列的底层是什么?听着,我脱口而出说是队列。每次从头上取下味精,面试官就觉得真是先进先出的队列,于是反问了我。请再考虑一下。队列还是链表?然后说不是队列。他让我再看一次

  booleanequeuemessage(message msg,long when)…/synchronized(this)if)m退出)/中止时,直接illegalstateeexceptione=log。w(tag,e.getMessage),e);味精。回收(;返回假;}味精。使用中的标记(;当=当消息p=消息;布尔新闻工作;if(p==null when==0 when p . when){//新头,wakeuptheeventqueueifblocked。味精。next=p;mMessages=msg .need wake=MB locked } else {//insertedwithinmiddleofthequeue。需要唤醒/唤醒事件队列除非有barrierattheheheheadofttttheheheadofttte和队列中的themessagestheearliestasynchronousmessage消息。需要唤醒消息prevfor(;({ prev=p;p=p . nextif (p==空 when p . when){ break;}如果(需要wakep。isa synchronous()){ need wake=false;} }味精。next=p;//不变量:p==prev。下一页。next=msg}//我们可以假设mPtr!=0因为mquittingisfalse.if(需要唤醒)原生wake)mptr);} }返回真;明明知道(天哪我这个hp,msg=queue.next),却依赖

  messagenext(…//省略int pendingIdleHandlerCount=-1;//-1仅在迭代期间//1 .nextpolltimeoutmillis=-1的话,即使一直屏蔽也不会超时。 //2 .如果nextpolltimeoutmillis=0,则不阻止并立即返回。 //3 .对于nextpolltimeoutmillis,最长为阻止nextpolltimeoutmillis的毫秒(超时)//如果在此期间有程序唤醒,则返回int nextPollTimeoutMillis=0;for(;(if ) nextpolltimeoutmillis!=0){ binder。flushpendingcommands(;}nativepollonce(ptr,nextPollTimeoutMillissynchronized(this)//trytoretrievethenextmessage。returniffound。finallongnow=系统时钟。正常运行时间(毫秒);消息上一个消息=空消息消息=消息如果target==null,则这是屏障并且必须循环遍历直到找到初始异步消息if(msg)=空消息。target==null(//stalledbyabarrier)msg=msg。接下来;}而(味精!=空!味精。isasynchronous();(如果)味精!=null(//如果有需要应对的消息,首先判断时间是否到了,如果没有的话设定阻塞时间。 //场景计算离常用的postdelayif(nowmsg.when )//运行时间还有多长时间才能分配给nextPollTimeoutMillis。nativePollOnce方法等待nextPollTimeoutMillis,然后nextPollTimeoutMillis=(int)math。最小(消息。when-now,integer.max_min ) //链表操作,获取味精,删除其节点if(prevmsg!=null(上一条消息。下一个=msg。接下来;} else { mMessages=msg . next } msg . next=nullmsg。使用中的标记(;//返回收到的消息的返回msg} } else { //无消息,nextPollTimeoutMillis重置nextPollTimeoutMillis=-1;()//省略) }消息队列顾名思义是消息队列,其操作主要有插入和读取。插入对应的方法是入队消息().也就是说,在消息队列中插入消息(对应的下一个).此方法从消息队列中检索消息,并将其从消息队列中删除。消息队列的名称包含队列,但底层实现采用了单链表。这是因为插入和删除链表的性能很好。

  正在看的这位前辈的代码和说明

  现在想想也很痛苦

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: