首页 > 编程知识 正文

python队列queue不堵塞,队列有几种

时间:2023-05-05 03:24:06 阅读:108385 作者:2617

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

booleanenqueuemessage (message msg,long when )…/synchronized (this ) if ) mquitting ) /中止时,直接illegalstateeeexceptione=log.w(tag,e.getMessage ),e ); msg.recycle (; 返回假; } msg.markInUse (; msg.when=when; Message p=mMessages; 布尔新闻工作; if (p==null|| when==0||whenp.when ) { //New head,wakeuptheeventqueueifblocked.msg.next=p; mMessages=msg; needWake=mBlocked; } else {//insertedwithinthemiddleofthequeue.usually we don ' thavetowake/uptheeventqueunlessthereisabarrierattheheheheadofttttheheheadofttte andthemessageistheearliestasynchronousmessageinthequeue.need wake message prev; for (; ({ prev=p; p=p.next; if (p==空||whenp.when ) { break; }if(Needwakep.isasynchronous () ) { needWake=false; } } msg.next=p; //invariant : p==prev.next prev.next=msg; } //We can assume mPtr!=0becausemquittingisfalse.if (need wake ) nativewake ) mptr ); } }返回真; 明明知道(天哪我这个hp,msg=queue.next ),却依赖

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

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

现在想想也很痛苦

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。