扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

Java涓璴inkedblockingdeque鎬庝箞璁剧疆瓒呮椂

扬州沐宇科技
2024-01-08 19:36:17
Java

鍦↗ava涓紝LinkedBlockingDeque绫绘槸涓€涓弻鍚戦摼琛ㄥ疄鐜扮殑闃诲闃熷垪锛屽彲浠ョ敤浜庡疄鐜扮敓浜ц€?娑堣垂鑰呮ā寮忋€侺inkedBlockingDeque绫绘病鏈夋彁渚涚洿鎺ョ殑瓒呮椂璁剧疆鏂规硶锛屼絾鍙互閫氳繃浣跨敤take()鍜宲oll()鏂规硶鏉ュ疄鐜拌秴鏃躲€?/p>

  1. 浣跨敤take()鏂规硶锛歵ake()鏂规硶浼氫竴鐩撮樆濉烇紝鐩村埌闃熷垪涓湁鍏冪礌鍙秷璐广€傚彲浠ュ湪take()鏂规硶涓婁娇鐢╰ry-catch鍧楁潵鎹曡幏InterruptedException寮傚父锛屽苟鍦╟atch鍧椾腑璁剧疆瓒呮椂閫昏緫銆?/li>
try {
    T element = deque.take();  // 闃诲鐩村埌闃熷垪闈炵┖
    // 澶勭悊闃熷垪鍏冪礌
} catch (InterruptedException e) {
    // 瓒呮椂閫昏緫
}
  1. 浣跨敤poll()鏂规硶锛歱oll()鏂规硶灏嗕粠闃熷垪澶撮儴鑾峰彇骞剁Щ闄や竴涓厓绱狅紝濡傛灉闃熷垪涓虹┖锛屽垯杩斿洖null銆傚彲浠ュ湪poll()鏂规硶涓婁娇鐢╰ry-catch鍧楁潵鎹曡幏InterruptedException寮傚父锛屽苟鍦╟atch鍧椾腑璁剧疆瓒呮椂閫昏緫銆?/li>
try {
    T element = deque.poll(timeout, TimeUnit.MILLISECONDS);  // 闃诲timeout姣鐩村埌闃熷垪闈炵┖
    if (element != null) {
        // 澶勭悊闃熷垪鍏冪礌
    } else {
        // 瓒呮椂閫昏緫
    }
} catch (InterruptedException e) {
    // 瓒呮椂閫昏緫
}

鍏朵腑锛宼imeout鏄瓑寰呰秴鏃剁殑鏃堕棿锛孴imeUnit.MILLISECONDS鏄椂闂村崟浣嶏紝鍙互鏍规嵁闇€瑕侀€夋嫨鍚堥€傜殑鏃堕棿鍗曚綅銆傚鏋滃湪鎸囧畾鐨勮秴鏃舵椂闂村唴闃熷垪涓病鏈夊彲鐢ㄥ厓绱狅紝鍒欎細瑙﹀彂瓒呮椂閫昏緫澶勭悊銆?/p>

扫码添加客服微信