-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBatchingCounter.java
38 lines (32 loc) · 1.23 KB
/
BatchingCounter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.thread.concurrency.counter;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
public class BatchingCounter implements Counter {
private static final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private static final ConcurrentLinkedQueue<Integer> jobQueue = new ConcurrentLinkedQueue<>();
private static volatile int count = 100;
public BatchingCounter() {
Runnable runnableTask = () -> {
while (!jobQueue.isEmpty()) {
synchronized (this) {
var value = jobQueue.poll();
count += value == null ? 0 : value;
}
}
};
// context switching을 최소화하는 최소한의 시간마다 실행하여 성능 향상
scheduledExecutorService.scheduleAtFixedRate(runnableTask, 4, 5, TimeUnit.MILLISECONDS);
}
@Override
public void add(int value) {
jobQueue.add(value);
}
@Override
public int show() {
return count;
}
}