Skip to content

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

Merged
merged 77 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
b0222f8
✨ Feat : 새로운 기능
haxr369 Mar 26, 2024
fd93056
✅ Test : 테스트 코드 추가
haxr369 Mar 27, 2024
3c8ff75
✨ Feat : 새로운 기능
haxr369 Mar 27, 2024
7660cf7
Update README.md
ooMia Mar 27, 2024
0b829d6
Merge branch 'main' into main
haxr369 Mar 28, 2024
1305987
Merge remote-tracking branch 'origin/main'
haxr369 Mar 28, 2024
c734c16
✨ Feat : 새로운 기능
haxr369 Mar 28, 2024
4e7a0fa
✨ Feat : `BasicCounter`
ooMia Mar 29, 2024
df915fb
✨ Feat : `Counter` 구현체
ooMia Mar 29, 2024
3f8bcb7
✅ Test : `@ParameterizedTest` for Counters
ooMia Mar 29, 2024
208827f
🚚 Chore : set coverage target
ooMia Mar 29, 2024
8a14371
Merge branch 'develop' into documentation
ooMia Mar 29, 2024
ed4626c
Merge branch 'main' into develop
ooMia Mar 29, 2024
59ee6bf
Merge branch 'develop' into documentation
ooMia Mar 29, 2024
dd839e6
📝 Docs : Update badges
ooMia Mar 29, 2024
7b499d3
Merge branch 'develop' into feature/counter
haxr369 Mar 30, 2024
f78aeac
✨ Feat : 새로운 기능
haxr369 Mar 30, 2024
b96f482
🤖 Refactor : 코드 리팩토링
haxr369 Mar 30, 2024
f528555
✨ Feat : 새로운 기능
haxr369 Mar 30, 2024
8c63b49
Merge remote-tracking branch 'concurrency/develop'
haxr369 Mar 30, 2024
2b28fd9
🚚 Chore : `codecov` 설정 변경
ooMia Mar 31, 2024
b77d483
✅ Test : `stressTest` 작성
ooMia Apr 1, 2024
83c6f93
✨ Feat : 새로운 기능
haxr369 Apr 2, 2024
0b33222
✅ Test : 테스트 코드 추가
haxr369 Apr 2, 2024
06cbe1f
🤖 Refactor : 코드 리팩토링
haxr369 Apr 2, 2024
a752e80
🐛 Fix : 오류 수정
haxr369 Apr 3, 2024
6494b66
✨ Feat : 새로운 기능
haxr369 Apr 3, 2024
db113d7
✅ Test : 테스트 코드 추가
haxr369 Apr 3, 2024
d7e8a86
🤖 Refactor : 코드 리팩토링
haxr369 Apr 3, 2024
a6870ac
🤖 Refactor : 코드 리팩토링
haxr369 Apr 3, 2024
35d3cbf
Integrated develop (#32)
ooMia Apr 3, 2024
19b0081
✨ Feat : `Counter` (#28)
ooMia Apr 3, 2024
fd67711
📝 Docs : 문서 수정
haxr369 Apr 4, 2024
1100a85
@haxr369 카운터 밴치 마크 측정
haxr369 Apr 4, 2024
d9ec163
✨ Feat : `ConcurrentBatchCounter`
ooMia Apr 4, 2024
7b074ac
✅ Test : `BatchCounter` 테스트
ooMia Apr 5, 2024
e4e68ed
🤖 Refactor : Simplify logic
ooMia Apr 5, 2024
a6ab129
🚚 Chore : set profile
ooMia Apr 5, 2024
61eb939
🎨 Style : polishing
ooMia Apr 5, 2024
f98cdbd
✅ Test : 테스트 코드 추가
haxr369 Apr 5, 2024
7860d1d
📝 Docs : `ConcurrentBatchingCounter` 벤치마킹
ooMia Apr 5, 2024
1bb0cc0
✨ Feat : 'ConcurrentBatchingCounter' 벤치마킹
ooMia Apr 5, 2024
2d1cdd0
Update README.md (#23)
ooMia Apr 5, 2024
6d1377d
✨ Feat : Implements `ConcurrentBatchCounter` (#34)
ooMia Apr 5, 2024
8eba8ee
🤖 Refactor : 코드 리팩토링
haxr369 Apr 6, 2024
e0edb4f
✅ Test : clean-up tests
ooMia Apr 6, 2024
90567f9
🎨 Style : polishing
ooMia Apr 6, 2024
2b1356c
🔥 Remove : `BasicCounter`
ooMia Apr 6, 2024
867a7b8
🚚 Chore : simplify `main`
ooMia Apr 6, 2024
5822bed
✅ Test : simplify tests (#35)
ooMia Apr 7, 2024
c763bb4
🤖 Refactor : 코드 리팩토링
haxr369 Apr 7, 2024
ada5ae2
🤖 Refactor : 코드 리팩토링
haxr369 Apr 7, 2024
b286a26
✅ Test : 테스트 코드 추가
haxr369 Apr 7, 2024
eb1c711
refacto/스레드 수 정돈
haxr369 Apr 7, 2024
66d4ccd
Merge branch 'integrated_develop'
haxr369 Apr 7, 2024
86554d6
기존 작업과 병합
haxr369 Apr 7, 2024
d49a951
Merge remote-tracking branch 'concurrency/develop'
haxr369 Apr 7, 2024
55a797d
sol 커밋 정리
haxr369 Apr 7, 2024
84da0f4
queueCounter는 counter와 다른 인터페이스를 공유하기 때문에 counter 패키지에서 삭제
haxr369 Apr 7, 2024
e783deb
🤖 Refactor : 코드 리팩토링
haxr369 Apr 7, 2024
75b0f90
존재하지 않는 구현제 제거
haxr369 Apr 7, 2024
e962db2
필요 없는 출력 제거
haxr369 Apr 7, 2024
1ea7290
블로킹 큐 구현체 완성 및 테스트 작성 완료 (#36)
ooMia Apr 7, 2024
3b3f5cd
🎨 Style : polishing
ooMia Apr 7, 2024
177c764
🤖 Refactor : refactor
ooMia Apr 8, 2024
ec44590
🔥 Remove : remove async examples
ooMia Apr 8, 2024
d2eed84
🎨 Style : polishing
ooMia Apr 8, 2024
2b55fc4
🔧 Modify : rename package
ooMia Apr 8, 2024
00a591d
✨ Feat : implements `clear()`
ooMia Apr 9, 2024
26429ba
🚚 Chore (QueueCounter) : set profile `dev`
ooMia Apr 9, 2024
bad08f5
✨ Feat (Counter) : custom test executor
ooMia Apr 9, 2024
130eaf8
🐛 Fix : remove redundancy
ooMia Apr 9, 2024
4f5af6c
📝 Docs : benchmark results
ooMia Apr 9, 2024
c2faebc
🎨 Style : polishing
ooMia Apr 9, 2024
056e144
✅ Test : simply check benchmark
ooMia Apr 10, 2024
d2f9fac
🚚 Chore : make test runtime shorter
ooMia Apr 10, 2024
1fe90f4
✨ Feat : test `clear()`
ooMia Apr 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
@@ -24,15 +25,17 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
3 changes: 2 additions & 1 deletion .github/workflows/gradle-test-main.yml
Original file line number Diff line number Diff line change
@@ -28,5 +28,6 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: spring-templates/spring-concurrency-thread
fail_ci_if_error: true
flags: integration
fail_ci_if_error: true
verbose: true
11 changes: 6 additions & 5 deletions .github/workflows/gradle-test.yml
Original file line number Diff line number Diff line change
@@ -2,8 +2,9 @@ name: Run gradlew test

on:
pull_request:
branches-ignore:
- main
branches:
- develop
- feature/**

jobs:
build:
@@ -36,6 +37,6 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: spring-templates/spring-concurrency-thread
fail_ci_if_error: true
verbose: true
flags: unittests
fail_ci_if_error: true
verbose: true
flags: ${{ github.ref == 'refs/pull/develop' && 'integration' || 'unittests' }}
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HELP.md
.gradle
build/
.gradle/*
build/*
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
@@ -18,7 +18,7 @@ bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
.idea/*
*.iws
*.iml
*.ipr
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# spring-thread-concurrency
[![codecov](https://codecov.io/gh/spring-templates/spring-concurrency-thread/graph/badge.svg?token=N3GEH8C5K7)](https://codecov.io/gh/spring-templates/spring-concurrency-thread)

![Spring Boot](https://img.shields.io/badge/Spring%20Boot-6DB33F?logo=springboot&logoColor=white)
![Java](https://img.shields.io/badge/Java-ED8B00?logoColor=white)
![Gradle](https://img.shields.io/badge/Gradle-02303A?logo=gradle&logoColor=white)
![JUnit5](https://img.shields.io/badge/JUnit5-25A162?logo=junit5&logoColor=white)
![JaCoCo](https://img.shields.io/badge/JaCoCo-D22128?logo=jacoco&logoColor=white)
![Codecov](https://img.shields.io/badge/Codecov-F01F7A?logo=codecov&logoColor=white)
![GitHub Actions](https://img.shields.io/badge/GitHub%20Actions-2088FF?logo=githubactions&logoColor=white)

# 관심사

- [멀티 스레드의 자원 공유 문제](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 |
10 changes: 10 additions & 0 deletions codecov.yml
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%
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 src/main/java/com/thread/concurrency/counter/AtomicCounter.java
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);
}
}
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);
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/thread/concurrency/counter/Counter.java
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 src/main/java/com/thread/concurrency/counter/LockCounter.java
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 src/main/java/com/thread/concurrency/counter/PollingCounter.java
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;
}
}
}
}
}
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;
}
}
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());
}
}
}
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();
}
Loading