自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java阻塞隊(duì)列實(shí)現(xiàn)原理分析

開發(fā) 后端
本文以ArrayBlockingQueue和LinkedBlockingQueue為例,分析它們的實(shí)現(xiàn)原理。

Java 阻塞隊(duì)列實(shí)現(xiàn)原理分析

Java中的阻塞隊(duì)列接口BlockingQueue繼承自Queue接口。

BlockingQueue接口提供了3個(gè)添加元素方法:

  • add:添加元素到隊(duì)列里,添加成功返回true,由于容量滿了添加失敗會(huì)拋出IllegalStateException異常;
  • offer:添加元素到隊(duì)列里,添加成功返回true,添加失敗返回false;
  • put:添加元素到隊(duì)列里,如果容量滿了會(huì)阻塞直到容量不滿。

3個(gè)刪除方法:

  • poll:刪除隊(duì)列頭部元素,如果隊(duì)列為空,返回null。否則返回元素;
  • remove:基于對象找到對應(yīng)的元素,并刪除。刪除成功返回true,否則返回false;
  • take:刪除隊(duì)列頭部元素,如果隊(duì)列為空,一直阻塞到隊(duì)列有元素并刪除。

常用的阻塞隊(duì)列具體類有ArrayBlockingQueue、LinkedBlockingQueue、PriorityBlockingQueue、LinkedBlockingDeque等。

本文以ArrayBlockingQueue和LinkedBlockingQueue為例,分析它們的實(shí)現(xiàn)原理。

ArrayBlockingQueue

ArrayBlockingQueue的原理就是使用一個(gè)可重入鎖和這個(gè)鎖生成的兩個(gè)條件對象進(jìn)行并發(fā)控制(classic two-condition algorithm)。

ArrayBlockingQueue是一個(gè)帶有長度的阻塞隊(duì)列,初始化的時(shí)候必須要指定隊(duì)列長度,且指定長度之后不允許進(jìn)行修改。

它帶有的屬性如下:

  1. // 存儲隊(duì)列元素的數(shù)組,是個(gè)循環(huán)數(shù)組 
  2.  
  3. final Object[] items; 
  4.  
  5.   
  6.  
  7. // 拿數(shù)據(jù)的索引,用于take,poll,peek,remove方法 
  8.  
  9. int takeIndex; 
  10.  
  11.   
  12.  
  13. // 放數(shù)據(jù)的索引,用于put,offer,add方法 
  14.  
  15. int putIndex; 
  16.  
  17.   
  18.  
  19. // 元素個(gè)數(shù) 
  20.  
  21. int count
  22.  
  23.   
  24.  
  25. // 可重入鎖 
  26.  
  27. final ReentrantLock lock; 
  28.  
  29. // notEmpty條件對象,由lock創(chuàng)建 
  30.  
  31. private final Condition notEmpty; 
  32.  
  33. // notFull條件對象,由lock創(chuàng)建 
  34.  
  35. private final Condition notFull;  

數(shù)據(jù)的添加

ArrayBlockingQueue有不同的幾個(gè)數(shù)據(jù)添加方法,add、offer、put方法。

add方法:

  1. public boolean add(E e) { 
  2.  
  3.     if (offer(e)) 
  4.  
  5.         return true
  6.  
  7.     else 
  8.  
  9.         throw new IllegalStateException("Queue full"); 
  10.  
  11.  

add方法內(nèi)部調(diào)用offer方法如下:

  1. public boolean offer(E e) { 
  2.  
  3.     checkNotNull(e); // 不允許元素為空 
  4.  
  5.     final ReentrantLock lock = this.lock; 
  6.  
  7.     lock.lock(); // 加鎖,保證調(diào)用offer方法的時(shí)候只有1個(gè)線程 
  8.  
  9.     try { 
  10.  
  11.         if (count == items.length) // 如果隊(duì)列已滿 
  12.  
  13.             return false; // 直接返回false,添加失敗 
  14.  
  15.         else { 
  16.  
  17.             insert(e); // 數(shù)組沒滿的話調(diào)用insert方法 
  18.  
  19.             return true; // 返回true,添加成功 
  20.  
  21.         } 
  22.  
  23.     } finally { 
  24.  
  25.         lock.unlock(); // 釋放鎖,讓其他線程可以調(diào)用offer方法 
  26.  
  27.     } 
  28.  
  29.  

insert方法如下:

  1. private void insert(E x) { 
  2.  
  3.     items[putIndex] = x; // 元素添加到數(shù)組里 
  4.  
  5.     putIndex = inc(putIndex); // 放數(shù)據(jù)索引+1,當(dāng)索引滿了變成0 
  6.  
  7.     ++count; // 元素個(gè)數(shù)+1 
  8.  
  9.     notEmpty.signal(); // 使用條件對象notEmpty通知,比如使用take方法的時(shí)候隊(duì)列里沒有數(shù)據(jù),被阻塞。這個(gè)時(shí)候隊(duì)列insert了一條數(shù)據(jù),需要調(diào)用signal進(jìn)行通知 
  10.  
  11.  

put方法:

  1. public void put(E e) throws InterruptedException { 
  2.  
  3.     checkNotNull(e); // 不允許元素為空 
  4.  
  5.     final ReentrantLock lock = this.lock; 
  6.  
  7.     lock.lockInterruptibly(); // 加鎖,保證調(diào)用put方法的時(shí)候只有1個(gè)線程 
  8.  
  9.     try { 
  10.  
  11.         while (count == items.length) // 如果隊(duì)列滿了,阻塞當(dāng)前線程,并加入到條件對象notFull的等待隊(duì)列里 
  12.  
  13.             notFull.await(); // 線程阻塞并被掛起,同時(shí)釋放鎖 
  14.  
  15.         insert(e); // 調(diào)用insert方法 
  16.  
  17.     } finally { 
  18.  
  19.         lock.unlock(); // 釋放鎖,讓其他線程可以調(diào)用put方法 
  20.  
  21.     } 
  22.  
  23.  

ArrayBlockingQueue的添加數(shù)據(jù)方法有add,put,offer這3個(gè)方法,總結(jié)如下:

add方法內(nèi)部調(diào)用offer方法,如果隊(duì)列滿了,拋出IllegalStateException異常,否則返回true

offer方法如果隊(duì)列滿了,返回false,否則返回true

add方法和offer方法不會(huì)阻塞線程,put方法如果隊(duì)列滿了會(huì)阻塞線程,直到有線程消費(fèi)了隊(duì)列里的數(shù)據(jù)才有可能被喚醒。

這3個(gè)方法內(nèi)部都會(huì)使用可重入鎖保證原子性。

數(shù)據(jù)的刪除

ArrayBlockingQueue有不同的幾個(gè)數(shù)據(jù)刪除方法,poll、take、remove方法。

poll方法:

  1. public E poll() { 
  2.  
  3.     final ReentrantLock lock = this.lock; 
  4.  
  5.     lock.lock(); // 加鎖,保證調(diào)用poll方法的時(shí)候只有1個(gè)線程 
  6.  
  7.     try { 
  8.  
  9.         return (count == 0) ? null : extract(); // 如果隊(duì)列里沒元素了,返回null,否則調(diào)用extract方法 
  10.  
  11.     } finally { 
  12.  
  13.         lock.unlock(); // 釋放鎖,讓其他線程可以調(diào)用poll方法 
  14.  
  15.     } 
  16.  
  17.  

poll方法內(nèi)部調(diào)用extract方法:

  1. private E extract() { 
  2.  
  3.     final Object[] items = this.items; 
  4.  
  5.     E x = this.<E>cast(items[takeIndex]); // 得到取索引位置上的元素 
  6.  
  7.     items[takeIndex] = null; // 對應(yīng)取索引上的數(shù)據(jù)清空 
  8.  
  9.     takeIndex = inc(takeIndex); // 取數(shù)據(jù)索引+1,當(dāng)索引滿了變成0 
  10.  
  11.     --count; // 元素個(gè)數(shù)-1 
  12.  
  13.     notFull.signal(); // 使用條件對象notFull通知,比如使用put方法放數(shù)據(jù)的時(shí)候隊(duì)列已滿,被阻塞。這個(gè)時(shí)候消費(fèi)了一條數(shù)據(jù),隊(duì)列沒滿了,就需要調(diào)用signal進(jìn)行通知 
  14.  
  15.     return x; // 返回元素 
  16.  
  17.  

take方法:

  1. public E take() throws InterruptedException { 
  2.  
  3.     final ReentrantLock lock = this.lock; 
  4.  
  5.     lock.lockInterruptibly(); // 加鎖,保證調(diào)用take方法的時(shí)候只有1個(gè)線程 
  6.  
  7.     try { 
  8.  
  9.         while (count == 0) // 如果隊(duì)列空,阻塞當(dāng)前線程,并加入到條件對象notEmpty的等待隊(duì)列里 
  10.  
  11.             notEmpty.await(); // 線程阻塞并被掛起,同時(shí)釋放鎖 
  12.  
  13.         return extract(); // 調(diào)用extract方法 
  14.  
  15.     } finally { 
  16.  
  17.         lock.unlock(); // 釋放鎖,讓其他線程可以調(diào)用take方法 
  18.  
  19.     } 
  20.  
  21.  

remove方法:

  1. public boolean remove(Object o) { 
  2.  
  3.     if (o == nullreturn false
  4.  
  5.     final Object[] items = this.items; 
  6.  
  7.     final ReentrantLock lock = this.lock; 
  8.  
  9.     lock.lock(); // 加鎖,保證調(diào)用remove方法的時(shí)候只有1個(gè)線程 
  10.  
  11.     try { 
  12.  
  13.         for (int i = takeIndex, k = count; k > 0; i = inc(i), k--) { // 遍歷元素 
  14.  
  15.             if (o.equals(items[i])) { // 兩個(gè)對象相等的話 
  16.  
  17.                 removeAt(i); // 調(diào)用removeAt方法 
  18.  
  19.                 return true; // 刪除成功,返回true 
  20.  
  21.             } 
  22.  
  23.         } 
  24.  
  25.         return false; // 刪除成功,返回false 
  26.  
  27.     } finally { 
  28.  
  29.         lock.unlock(); // 釋放鎖,讓其他線程可以調(diào)用remove方法 
  30.  
  31.     } 
  32.  
  33.  

removeAt方法:

  1. void removeAt(int i) { 
  2.  
  3.     final Object[] items = this.items; 
  4.  
  5.     if (i == takeIndex) { // 如果要?jiǎng)h除數(shù)據(jù)的索引是取索引位置,直接刪除取索引位置上的數(shù)據(jù),然后取索引+1即可 
  6.  
  7.         items[takeIndex] = null
  8.  
  9.         takeIndex = inc(takeIndex); 
  10.  
  11.     } else { // 如果要?jiǎng)h除數(shù)據(jù)的索引不是取索引位置,移動(dòng)元素元素,更新取索引和放索引的值 
  12.  
  13.         for (;;) { 
  14.  
  15.             int nexti = inc(i); 
  16.  
  17.             if (nexti != putIndex) { 
  18.  
  19.                 items[i] = items[nexti]; 
  20.  
  21.                 i = nexti; 
  22.  
  23.             } else { 
  24.  
  25.                 items[i] = null
  26.  
  27.                 putIndex = i; 
  28.  
  29.                 break; 
  30.  
  31.             } 
  32.  
  33.         } 
  34.  
  35.     } 
  36.  
  37.     --count; // 元素個(gè)數(shù)-1 
  38.  
  39.     notFull.signal(); // 使用條件對象notFull通知,比如使用put方法放數(shù)據(jù)的時(shí)候隊(duì)列已滿,被阻塞。這個(gè)時(shí)候消費(fèi)了一條數(shù)據(jù),隊(duì)列沒滿了,就需要調(diào)用signal進(jìn)行通知  
  40.  
  41.  

ArrayBlockingQueue的刪除數(shù)據(jù)方法有poll,take,remove這3個(gè)方法,總結(jié)如下:

poll方法對于隊(duì)列為空的情況,返回null,否則返回隊(duì)列頭部元素。

remove方法取的元素是基于對象的下標(biāo)值,刪除成功返回true,否則返回false。

poll方法和remove方法不會(huì)阻塞線程。

take方法對于隊(duì)列為空的情況,會(huì)阻塞并掛起當(dāng)前線程,直到有數(shù)據(jù)加入到隊(duì)列中。

這3個(gè)方法內(nèi)部都會(huì)調(diào)用notFull.signal方法通知正在等待隊(duì)列滿情況下的阻塞線程。

LinkedBlockingQueue

LinkedBlockingQueue是一個(gè)使用鏈表完成隊(duì)列操作的阻塞隊(duì)列。鏈表是單向鏈表,而不是雙向鏈表。

內(nèi)部使用放鎖和拿鎖,這兩個(gè)鎖實(shí)現(xiàn)阻塞(“two lock queue” algorithm)。

它帶有的屬性如下:

  1. // 容量大小 
  2.  
  3. private final int capacity; 
  4.  
  5.   
  6.  
  7. // 元素個(gè)數(shù),因?yàn)橛?個(gè)鎖,存在競態(tài)條件,使用AtomicInteger 
  8.  
  9. private final AtomicInteger count = new AtomicInteger(0); 
  10.  
  11.   
  12.  
  13. // 頭結(jié)點(diǎn) 
  14.  
  15. private transient Node<E> head; 
  16.  
  17.   
  18.  
  19. // 尾節(jié)點(diǎn) 
  20.  
  21. private transient Node<E> last
  22.  
  23.   
  24.  
  25. // 拿鎖 
  26.  
  27. private final ReentrantLock takeLock = new ReentrantLock(); 
  28.  
  29.   
  30.  
  31. // 拿鎖的條件對象 
  32.  
  33. private final Condition notEmpty = takeLock.newCondition(); 
  34.  
  35.   
  36.  
  37. // 放鎖 
  38.  
  39. private final ReentrantLock putLock = new ReentrantLock(); 
  40.  
  41.   
  42.  
  43. // 放鎖的條件對象 
  44.  
  45. private final Condition notFull = putLock.newCondition();  

ArrayBlockingQueue只有1個(gè)鎖,添加數(shù)據(jù)和刪除數(shù)據(jù)的時(shí)候只能有1個(gè)被執(zhí)行,不允許并行執(zhí)行。

而LinkedBlockingQueue有2個(gè)鎖,放鎖和拿鎖,添加數(shù)據(jù)和刪除數(shù)據(jù)是可以并行進(jìn)行的,當(dāng)然添加數(shù)據(jù)和刪除數(shù)據(jù)的時(shí)候只能有1個(gè)線程各自執(zhí)行。

數(shù)據(jù)的添加

LinkedBlockingQueue有不同的幾個(gè)數(shù)據(jù)添加方法,add、offer、put方法。

add方法內(nèi)部調(diào)用offer方法:

  1. public boolean offer(E e) { 
  2.  
  3.     if (e == null) throw new NullPointerException(); // 不允許空元素 
  4.  
  5.     final AtomicInteger count = this.count
  6.  
  7.     if (count.get() == capacity) // 如果容量滿了,返回false 
  8.  
  9.         return false
  10.  
  11.     int c = -1; 
  12.  
  13.     Node<E> node = new Node(e); // 容量沒滿,以新元素構(gòu)造節(jié)點(diǎn) 
  14.  
  15.     final ReentrantLock putLock = this.putLock; 
  16.  
  17.     putLock.lock(); // 放鎖加鎖,保證調(diào)用offer方法的時(shí)候只有1個(gè)線程 
  18.  
  19.     try { 
  20.  
  21.         if (count.get() < capacity) { // 再次判斷容量是否已滿,因?yàn)榭赡苣面i在進(jìn)行消費(fèi)數(shù)據(jù),沒滿的話繼續(xù)執(zhí)行 
  22.  
  23.             enqueue(node); // 節(jié)點(diǎn)添加到鏈表尾部 
  24.  
  25.             c = count.getAndIncrement(); // 元素個(gè)數(shù)+1 
  26.  
  27.             if (c + 1 < capacity) // 如果容量還沒滿 
  28.  
  29.                 notFull.signal(); // 在放鎖的條件對象notFull上喚醒正在等待的線程,表示可以再次往隊(duì)列里面加數(shù)據(jù)了,隊(duì)列還沒滿 
  30.  
  31.         } 
  32.  
  33.     } finally { 
  34.  
  35.         putLock.unlock(); // 釋放放鎖,讓其他線程可以調(diào)用offer方法 
  36.  
  37.     } 
  38.  
  39.     if (c == 0) // 由于存在放鎖和拿鎖,這里可能拿鎖一直在消費(fèi)數(shù)據(jù),count會(huì)變化。這里的if條件表示如果隊(duì)列中還有1條數(shù)據(jù) 
  40.  
  41.         signalNotEmpty(); // 在拿鎖的條件對象notEmpty上喚醒正在等待的1個(gè)線程,表示隊(duì)列里還有1條數(shù)據(jù),可以進(jìn)行消費(fèi) 
  42.  
  43.     return c >= 0; // 添加成功返回true,否則返回false 
  44.  
  45.  

put方法:

  1. public void put(E e) throws InterruptedException { 
  2.  
  3.     if (e == null) throw new NullPointerException(); // 不允許空元素 
  4.  
  5.     int c = -1; 
  6.  
  7.     Node<E> node = new Node(e); // 以新元素構(gòu)造節(jié)點(diǎn) 
  8.  
  9.     final ReentrantLock putLock = this.putLock; 
  10.  
  11.     final AtomicInteger count = this.count
  12.  
  13.     putLock.lockInterruptibly(); // 放鎖加鎖,保證調(diào)用put方法的時(shí)候只有1個(gè)線程 
  14.  
  15.     try { 
  16.  
  17.         while (count.get() == capacity) { // 如果容量滿了 
  18.  
  19.             notFull.await(); // 阻塞并掛起當(dāng)前線程 
  20.  
  21.         } 
  22.  
  23.         enqueue(node); // 節(jié)點(diǎn)添加到鏈表尾部 
  24.  
  25.         c = count.getAndIncrement(); // 元素個(gè)數(shù)+1 
  26.  
  27.         if (c + 1 < capacity) // 如果容量還沒滿 
  28.  
  29.             notFull.signal(); // 在放鎖的條件對象notFull上喚醒正在等待的線程,表示可以再次往隊(duì)列里面加數(shù)據(jù)了,隊(duì)列還沒滿 
  30.  
  31.     } finally { 
  32.  
  33.         putLock.unlock(); // 釋放放鎖,讓其他線程可以調(diào)用put方法 
  34.  
  35.     } 
  36.  
  37.     if (c == 0) // 由于存在放鎖和拿鎖,這里可能拿鎖一直在消費(fèi)數(shù)據(jù),count會(huì)變化。這里的if條件表示如果隊(duì)列中還有1條數(shù)據(jù) 
  38.  
  39.         signalNotEmpty(); // 在拿鎖的條件對象notEmpty上喚醒正在等待的1個(gè)線程,表示隊(duì)列里還有1條數(shù)據(jù),可以進(jìn)行消費(fèi) 
  40.  
  41.  

LinkedBlockingQueue的添加數(shù)據(jù)方法add,put,offer跟ArrayBlockingQueue一樣,不同的是它們的底層實(shí)現(xiàn)不一樣。

ArrayBlockingQueue中放入數(shù)據(jù)阻塞的時(shí)候,需要消費(fèi)數(shù)據(jù)才能喚醒。

而LinkedBlockingQueue中放入數(shù)據(jù)阻塞的時(shí)候,因?yàn)樗鼉?nèi)部有2個(gè)鎖,可以并行執(zhí)行放入數(shù)據(jù)和消費(fèi)數(shù)據(jù),不僅在消費(fèi)數(shù)據(jù)的時(shí)候進(jìn)行喚醒插入阻塞的線程,同時(shí)在插入的時(shí)候如果容量還沒滿,也會(huì)喚醒插入阻塞的線程。

數(shù)據(jù)的刪除

LinkedBlockingQueue有不同的幾個(gè)數(shù)據(jù)刪除方法,poll、take、remove方法。

poll方法:

  1. public E poll() { 
  2.  
  3.     final AtomicInteger count = this.count
  4.  
  5.     if (count.get() == 0) // 如果元素個(gè)數(shù)為0 
  6.  
  7.         return null; // 返回null 
  8.  
  9.     E x = null
  10.  
  11.     int c = -1; 
  12.  
  13.     final ReentrantLock takeLock = this.takeLock; 
  14.  
  15.     takeLock.lock(); // 拿鎖加鎖,保證調(diào)用poll方法的時(shí)候只有1個(gè)線程 
  16.  
  17.     try { 
  18.  
  19.         if (count.get() > 0) { // 判斷隊(duì)列里是否還有數(shù)據(jù) 
  20.  
  21.             x = dequeue(); // 刪除頭結(jié)點(diǎn) 
  22.  
  23.             c = count.getAndDecrement(); // 元素個(gè)數(shù)-1 
  24.  
  25.             if (c > 1) // 如果隊(duì)列里還有元素 
  26.  
  27.                 notEmpty.signal(); // 在拿鎖的條件對象notEmpty上喚醒正在等待的線程,表示隊(duì)列里還有數(shù)據(jù),可以再次消費(fèi) 
  28.  
  29.         } 
  30.  
  31.     } finally { 
  32.  
  33.         takeLock.unlock(); // 釋放拿鎖,讓其他線程可以調(diào)用poll方法 
  34.  
  35.     } 
  36.  
  37.     if (c == capacity) // 由于存在放鎖和拿鎖,這里可能放鎖一直在添加數(shù)據(jù),count會(huì)變化。這里的if條件表示如果隊(duì)列中還可以再插入數(shù)據(jù) 
  38.  
  39.         signalNotFull(); // 在放鎖的條件對象notFull上喚醒正在等待的1個(gè)線程,表示隊(duì)列里還能再次添加數(shù)據(jù) 
  40.  
  41.                 return x; 
  42.  

 

take方法:

  1. public E take() throws InterruptedException { 
  2.  
  3.     E x; 
  4.  
  5.     int c = -1; 
  6.  
  7.     final AtomicInteger count = this.count
  8.  
  9.     final ReentrantLock takeLock = this.takeLock; 
  10.  
  11.     takeLock.lockInterruptibly(); // 拿鎖加鎖,保證調(diào)用take方法的時(shí)候只有1個(gè)線程 
  12.  
  13.     try { 
  14.  
  15.         while (count.get() == 0) { // 如果隊(duì)列里已經(jīng)沒有元素了 
  16.  
  17.             notEmpty.await(); // 阻塞并掛起當(dāng)前線程 
  18.  
  19.         } 
  20.  
  21.         x = dequeue(); // 刪除頭結(jié)點(diǎn) 
  22.  
  23.         c = count.getAndDecrement(); // 元素個(gè)數(shù)-1 
  24.  
  25.         if (c > 1) // 如果隊(duì)列里還有元素 
  26.  
  27.             notEmpty.signal(); // 在拿鎖的條件對象notEmpty上喚醒正在等待的線程,表示隊(duì)列里還有數(shù)據(jù),可以再次消費(fèi) 
  28.  
  29.     } finally { 
  30.  
  31.         takeLock.unlock(); // 釋放拿鎖,讓其他線程可以調(diào)用take方法 
  32.  
  33.     } 
  34.  
  35.     if (c == capacity) // 由于存在放鎖和拿鎖,這里可能放鎖一直在添加數(shù)據(jù),count會(huì)變化。這里的if條件表示如果隊(duì)列中還可以再插入數(shù)據(jù) 
  36.  
  37.         signalNotFull(); // 在放鎖的條件對象notFull上喚醒正在等待的1個(gè)線程,表示隊(duì)列里還能再次添加數(shù)據(jù) 
  38.  
  39.     return x; 
  40.  

 

remove方法:

  1. public boolean remove(Object o) { 
  2.  
  3.     if (o == nullreturn false
  4.  
  5.     fullyLock(); // remove操作要移動(dòng)的位置不固定,2個(gè)鎖都需要加鎖 
  6.  
  7.     try { 
  8.  
  9.         for (Node<E> trail = head, p = trail.next; // 從鏈表頭結(jié)點(diǎn)開始遍歷 
  10.  
  11.              p != null
  12.  
  13.              trail = p, p = p.next) { 
  14.  
  15.             if (o.equals(p.item)) { // 判斷是否找到對象 
  16.  
  17.                 unlink(p, trail); // 修改節(jié)點(diǎn)的鏈接信息,同時(shí)調(diào)用notFull的signal方法 
  18.  
  19.                 return true
  20.  
  21.             } 
  22.  
  23.         } 
  24.  
  25.         return false
  26.  
  27.     } finally { 
  28.  
  29.         fullyUnlock(); // 2個(gè)鎖解鎖 
  30.  
  31.     } 
  32.  

 

LinkedBlockingQueue的take方法對于沒數(shù)據(jù)的情況下會(huì)阻塞,poll方法刪除鏈表頭結(jié)點(diǎn),remove方法刪除指定的對象。

需要注意的是remove方法由于要?jiǎng)h除的數(shù)據(jù)的位置不確定,需要2個(gè)鎖同時(shí)加鎖。

責(zé)任編輯:龐桂玉 來源: 程序源
相關(guān)推薦

2020-11-19 07:41:51

ArrayBlocki

2020-11-25 14:28:56

DelayedWork

2020-11-24 09:04:55

PriorityBlo

2020-11-20 06:22:02

LinkedBlock

2012-06-14 10:34:40

Java阻塞搜索實(shí)例

2024-07-16 18:05:19

延遲隊(duì)列MQRabbitMQ

2025-01-14 00:00:00

Blocking隊(duì)列元素

2023-12-05 13:46:09

解密協(xié)程線程隊(duì)列

2021-06-04 18:14:15

阻塞非阻塞tcp

2022-06-30 14:31:57

Java阻塞隊(duì)列

2018-10-31 15:54:47

Java線程池源碼

2012-04-11 15:41:48

JavaNIO

2021-03-01 23:31:48

隊(duì)列實(shí)現(xiàn)棧存儲

2009-03-26 13:43:59

實(shí)現(xiàn)Order ByMySQL

2021-07-12 09:17:54

Memory Comp系統(tǒng)內(nèi)存

2023-12-15 09:45:21

阻塞接口

2023-02-07 09:17:19

Java注解原理

2009-04-02 10:23:13

實(shí)現(xiàn)JoinMySQL

2023-10-13 00:09:20

桶排序排序算法

2017-02-27 10:43:07

Javasynchronize
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號