Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #191] VariableLinkedBlockingQueue的take、poll方法优化 #430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xingyiteng
Copy link

issue:#191 VariableLinkedBlockingQueue中的take、poll方法优化
原VariableLinkedBlockingQueue中take、poll方法代码:

if (c >= capacity) {
    signalNotFull();
}

修改后take、poll方法代码:

if (c == capacity) {
    signalNotFull();
}

原因:

  1. c 的含义是:在取出元素之前队列中的元素数量。参考代码:c = count.getAndDecrement();
  2. c == capacity 的含义是:如果取出元素之前,元素数量等于队列容量,那么在取出元素之后会有一个空位,则可以通知等待put操作的线程插入元素。
  3. c > capacity 的含义是:如果取出元素之前,元素数量大于队列容量,那么在取出元素之后依然没有空位,如果此时通知等待put操作的线程,则put操作依然失败。参考代码:while (count.get() >= capacity) {notFull.await();}

优化:

  1. 改为c == capacity,在高并发场景下,减少不必要的线程状态切换。

fix:VariableLinkedBlockingQueue的take、poll方法优化
@KamToHung
Copy link
Collaborator

if (c >= capacity)

如果c > capacity的时候signal not full确实有点不合理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants