baidu / baidu/uid-generator

AtomicLong.updateAndGet()超级费CPU,并发一多就100%

Open
#86 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.6k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

![Screenshot from 2023-03-03 15-26-20](https://user-images.githubusercontent.com/2627712/222658085-0f44f919-f859-401b-a30f-73acd0f2b394.png)
![Screenshot from 2023-03-03 15-27-17](https://user-images.githubusercontent.com/2627712/222658190-58242bb9-64e1-4beb-b1f1-a4feb04ed61f.png)

原来:
```
// spin get next available cursor
long currentCursor = cursor.get();
long nextCursor = cursor.updateAndGet(old -> old == tail.get() ? old : old + 1);

// trigger padding in an async-mode if reach the threshold
long currentTail = tail.get();
```

改成下面这样是否可以???
```
// spin get next available cursor
long currentCursor = cursor.get();
long currentTail = tail.get();

long nextCursor = currentCursor;
if (currentCursor != currentTail) {
nextCursor = cursor.incrementAndGet();
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start from the cursor updateAndGet() path shown in the issue and examine its behavior as concurrency increases. Compare the proposed get/increment sequence with the existing atomic update under contention, checking cursor and tail correctness as well as CPU usage. Done means the chosen approach is validated for concurrent behavior and the reported CPU problem is addressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.