-
Notifications
You must be signed in to change notification settings - Fork 1
Counter
예제 구현
#31
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
+868
−24
Merged
Counter
예제 구현
#31
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
b0222f8
✨ Feat : 새로운 기능
haxr369 fd93056
✅ Test : 테스트 코드 추가
haxr369 3c8ff75
✨ Feat : 새로운 기능
haxr369 7660cf7
Update README.md
ooMia 0b829d6
Merge branch 'main' into main
haxr369 1305987
Merge remote-tracking branch 'origin/main'
haxr369 c734c16
✨ Feat : 새로운 기능
haxr369 4e7a0fa
✨ Feat : `BasicCounter`
ooMia df915fb
✨ Feat : `Counter` 구현체
ooMia 3f8bcb7
✅ Test : `@ParameterizedTest` for Counters
ooMia 208827f
🚚 Chore : set coverage target
ooMia 8a14371
Merge branch 'develop' into documentation
ooMia ed4626c
Merge branch 'main' into develop
ooMia 59ee6bf
Merge branch 'develop' into documentation
ooMia dd839e6
📝 Docs : Update badges
ooMia 7b499d3
Merge branch 'develop' into feature/counter
haxr369 f78aeac
✨ Feat : 새로운 기능
haxr369 b96f482
🤖 Refactor : 코드 리팩토링
haxr369 f528555
✨ Feat : 새로운 기능
haxr369 8c63b49
Merge remote-tracking branch 'concurrency/develop'
haxr369 2b28fd9
🚚 Chore : `codecov` 설정 변경
ooMia b77d483
✅ Test : `stressTest` 작성
ooMia 83c6f93
✨ Feat : 새로운 기능
haxr369 0b33222
✅ Test : 테스트 코드 추가
haxr369 06cbe1f
🤖 Refactor : 코드 리팩토링
haxr369 a752e80
🐛 Fix : 오류 수정
haxr369 6494b66
✨ Feat : 새로운 기능
haxr369 db113d7
✅ Test : 테스트 코드 추가
haxr369 d7e8a86
🤖 Refactor : 코드 리팩토링
haxr369 a6870ac
🤖 Refactor : 코드 리팩토링
haxr369 35d3cbf
Integrated develop (#32)
ooMia 19b0081
✨ Feat : `Counter` (#28)
ooMia fd67711
📝 Docs : 문서 수정
haxr369 1100a85
@haxr369 카운터 밴치 마크 측정
haxr369 d9ec163
✨ Feat : `ConcurrentBatchCounter`
ooMia 7b074ac
✅ Test : `BatchCounter` 테스트
ooMia e4e68ed
🤖 Refactor : Simplify logic
ooMia a6ab129
🚚 Chore : set profile
ooMia 61eb939
🎨 Style : polishing
ooMia f98cdbd
✅ Test : 테스트 코드 추가
haxr369 7860d1d
📝 Docs : `ConcurrentBatchingCounter` 벤치마킹
ooMia 1bb0cc0
✨ Feat : 'ConcurrentBatchingCounter' 벤치마킹
ooMia 2d1cdd0
Update README.md (#23)
ooMia 6d1377d
✨ Feat : Implements `ConcurrentBatchCounter` (#34)
ooMia 8eba8ee
🤖 Refactor : 코드 리팩토링
haxr369 e0edb4f
✅ Test : clean-up tests
ooMia 90567f9
🎨 Style : polishing
ooMia 2b1356c
🔥 Remove : `BasicCounter`
ooMia 867a7b8
🚚 Chore : simplify `main`
ooMia 5822bed
✅ Test : simplify tests (#35)
ooMia c763bb4
🤖 Refactor : 코드 리팩토링
haxr369 ada5ae2
🤖 Refactor : 코드 리팩토링
haxr369 b286a26
✅ Test : 테스트 코드 추가
haxr369 eb1c711
refacto/스레드 수 정돈
haxr369 66d4ccd
Merge branch 'integrated_develop'
haxr369 86554d6
기존 작업과 병합
haxr369 d49a951
Merge remote-tracking branch 'concurrency/develop'
haxr369 55a797d
sol 커밋 정리
haxr369 84da0f4
queueCounter는 counter와 다른 인터페이스를 공유하기 때문에 counter 패키지에서 삭제
haxr369 e783deb
🤖 Refactor : 코드 리팩토링
haxr369 75b0f90
존재하지 않는 구현제 제거
haxr369 e962db2
필요 없는 출력 제거
haxr369 1ea7290
블로킹 큐 구현체 완성 및 테스트 작성 완료 (#36)
ooMia 3b3f5cd
🎨 Style : polishing
ooMia 177c764
🤖 Refactor : refactor
ooMia ec44590
🔥 Remove : remove async examples
ooMia d2eed84
🎨 Style : polishing
ooMia 2b55fc4
🔧 Modify : rename package
ooMia 00a591d
✨ Feat : implements `clear()`
ooMia 26429ba
🚚 Chore (QueueCounter) : set profile `dev`
ooMia bad08f5
✨ Feat (Counter) : custom test executor
ooMia 130eaf8
🐛 Fix : remove redundancy
ooMia 4f5af6c
📝 Docs : benchmark results
ooMia c2faebc
🎨 Style : polishing
ooMia 056e144
✅ Test : simply check benchmark
ooMia d2f9fac
🚚 Chore : make test runtime shorter
ooMia 1fe90f4
✨ Feat : test `clear()`
ooMia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,50 @@ | ||
# spring-thread-concurrency | ||
[](https://codecov.io/gh/spring-templates/spring-concurrency-thread) | ||
|
||
 | ||
 | ||
 | ||
 | ||
 | ||
 | ||
 | ||
|
||
# 관심사 | ||
|
||
- [멀티 스레드의 자원 공유 문제](https://github.com/spring-templates/spring-concurrency-thread/discussions/16) | ||
- [멀티 쓰레드 자원 업데이트 문제](https://github.com/spring-templates/spring-concurrency-thread/discussions/17) | ||
|
||
# 정보 | ||
|
||
- [동시성 기본 조건과 관심사](https://github.com/spring-templates/spring-concurrency-thread/discussions/2) | ||
|
||
# [Counter-implementation Benchmark](https://www.notion.so/softsquared/f314375356b54381a8878cf2dabd381b) | ||
|
||
> - median of 25 iterations | ||
> - nRequests: 2^21 - 1 | ||
|
||
| name | nThreads | time (ms) | memory (KB) | | ||
|-------------------|----------|-----------|-------------| | ||
| AtomicBatch | 4 | 12 | 480 | | ||
| Atomic | 1 | 14 | 318 | | ||
| AtomicBatch | 1 | 30 | 240 | | ||
| Lock | 1 | 61 | 241 | | ||
| Synchronized | 1 | 61 | 241 | | ||
| Polling | 1 | 78 | 463 | | ||
| CompletableFuture | 1 | 158 | 25710 | | ||
|
||
### AtomicBatch vs Atomic | ||
|
||
> - nThreads: AtomicBatch=4, Atomic=1 | ||
|
||
| name | nRequests | time (ms) | memory (KB) | | ||
|-------------|-----------|-----------|-------------| | ||
| AtomicBatch | 2^21 - 1 | 12 | 480 | | ||
| AtomicBatch | 2^22 - 1 | 24 | 538 | | ||
| AtomicBatch | 2^23 - 1 | 42 | 572 | | ||
| AtomicBatch | 2^30 - 1 | 5695 | 511 | | ||
| AtomicBatch | 2^31 - 1 | 11621 | 294 | | ||
| Atomic | 2^21 - 1 | 14 | 318 | | ||
| Atomic | 2^22 - 1 | 27 | 244 | | ||
| Atomic | 2^23 - 1 | 55 | 344 | | ||
| Atomic | 2^30 - 1 | 7178 | 103 | | ||
| Atomic | 2^31 - 1 | 14377 | 266 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
coverage: | ||
status: | ||
project: | ||
default: | ||
target: 40% | ||
threshold: 10% | ||
patch: | ||
default: | ||
target: 30% | ||
threshold: 10% |
8 changes: 6 additions & 2 deletions
8
src/main/java/com/thread/concurrency/SpringThreadConcurrencyApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package com.thread.concurrency; | ||
|
||
import com.thread.concurrency.executor.CounterBenchmark; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
@SpringBootApplication | ||
public class SpringThreadConcurrencyApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringThreadConcurrencyApplication.class, args); | ||
ConfigurableApplicationContext context = SpringApplication.run(SpringThreadConcurrencyApplication.class, args); | ||
var performance = context.getBean(CounterBenchmark.class).benchmark(); | ||
System.out.println("|----------------------|---------------|---------------|---------------|"); | ||
System.out.println(performance); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/thread/concurrency/counter/AtomicCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.thread.concurrency.counter; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@Component | ||
public class AtomicCounter implements Counter { | ||
private final AtomicInteger count = new AtomicInteger(100); | ||
|
||
@Override | ||
public void add(int value) { | ||
count.addAndGet(value); | ||
} | ||
|
||
@Override | ||
public int show() { | ||
return count.get(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
count.set(0); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/thread/concurrency/counter/CompletableFutureCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.thread.concurrency.counter; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@Component | ||
public class CompletableFutureCounter implements Counter { | ||
private CompletableFuture<Integer> counter; | ||
|
||
public CompletableFutureCounter() { | ||
this.counter = new CompletableFuture<>(); | ||
counter.complete(100); | ||
} | ||
|
||
@Override | ||
public void add(int value) { | ||
synchronized (this) { | ||
counter = counter.thenApply((c) -> c + value); | ||
} | ||
} | ||
|
||
@Override | ||
public int show() { | ||
try { | ||
return counter.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
synchronized (this) { | ||
counter = CompletableFuture.completedFuture(0); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.thread.concurrency.counter; | ||
|
||
public interface Counter { | ||
void add(int value); | ||
|
||
int show(); | ||
|
||
void clear(); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/thread/concurrency/counter/LockCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.thread.concurrency.counter; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
@Component | ||
public class LockCounter implements Counter { | ||
private static final ReentrantLock lock = new ReentrantLock(); | ||
private static int count = 100; | ||
|
||
@Override | ||
public void add(int value) { | ||
lock.lock(); | ||
try { | ||
count += value; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public int show() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
lock.lock(); | ||
try { | ||
count = 0; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/thread/concurrency/counter/PollingCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.thread.concurrency.counter; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PollingCounter implements Counter { | ||
private static int count = 100; | ||
private static volatile boolean lock = false; | ||
|
||
@Override | ||
public void add(int value) { | ||
while (true) { | ||
if (!lock) { | ||
synchronized (PollingCounter.class) { | ||
lock = true; | ||
count += value; | ||
lock = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public int show() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
while (true) { | ||
if (!lock) { | ||
synchronized (PollingCounter.class) { | ||
lock = true; | ||
count = 0; | ||
lock = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/thread/concurrency/counter/SynchronizedCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.thread.concurrency.counter; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SynchronizedCounter implements Counter { | ||
|
||
private int counter = 100; | ||
|
||
@Override | ||
public synchronized void add(int value) { | ||
counter += value; | ||
} | ||
|
||
@Override | ||
public synchronized int show() { | ||
return counter; | ||
} | ||
|
||
@Override | ||
public synchronized void clear() { | ||
counter = 0; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/thread/concurrency/counter/batch/AtomicBatchCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.thread.concurrency.counter.batch; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.atomic.LongAdder; | ||
|
||
@Component | ||
public class AtomicBatchCounter implements BatchCounter { | ||
private final AtomicLong counter = new AtomicLong(); | ||
private final ConcurrentMap<Long, LongAdder> batch = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void add(int value) { | ||
var threadId = Thread.currentThread().threadId(); | ||
batch.computeIfAbsent(threadId, k -> new LongAdder()).add(value); | ||
} | ||
|
||
@Override | ||
public int show() { | ||
return counter.intValue(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
counter.set(0); | ||
batch.clear(); | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
var threadId = Thread.currentThread().threadId(); | ||
flush(threadId); | ||
} | ||
|
||
private void flush(long threadId) { | ||
var value = batch.remove(threadId); | ||
if (value != null) { | ||
counter.addAndGet(value.longValue()); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/thread/concurrency/counter/batch/BatchCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.thread.concurrency.counter.batch; | ||
|
||
import com.thread.concurrency.counter.Counter; | ||
|
||
public interface BatchCounter extends Counter { | ||
void flush(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.