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

Integrated develop #32

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 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,18 @@
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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);

Check warning on line 26 in src/main/java/com/thread/concurrency/counter/CompletableFutureCounter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/thread/concurrency/counter/CompletableFutureCounter.java#L25-L26

Added lines #L25 - L26 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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;
}
}
49 changes: 49 additions & 0 deletions src/test/java/com/thread/concurrency/AtomicCounterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.thread.concurrency;

import com.thread.concurrency.counter.AtomicCounter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.time.LocalTime;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootTest
public class AtomicCounterTest {
private final int counteNumber = 1;
private final int totalCount = 5000000;
private final int maxThreadNumber = 15;
private static final Logger logger = LoggerFactory.getLogger(SynchronizedCounterTest.class);
@Autowired
AtomicCounter counter;

@Test
@DisplayName("synchronized로 스레드 안전한 카운터로 동시에 여러 더하기 수행하기.")
public void 여러_더하기_수행_Executor() throws InterruptedException {

LocalTime lt1 = LocalTime.now();
int initalCount = counter.show();

ExecutorService service = Executors.newFixedThreadPool(maxThreadNumber);
CountDownLatch latch = new CountDownLatch(totalCount);
for (int i = 0; i < totalCount; i++) {
service.submit(() -> {
counter.add(counteNumber);
latch.countDown();
});
}
latch.await();
int finalCount = counter.show();
LocalTime lt2 = LocalTime.now();
long dif = Duration.between(lt1, lt2).getNano();
logger.info("여러_더하기_수행_Executor 테스트가 걸린 시간 : " + ((float)dif / 1000000) + "ms");
Assertions.assertEquals(initalCount + totalCount * counteNumber, finalCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.thread.concurrency;

import com.thread.concurrency.counter.CompletableFutureCounter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

@SpringBootTest
public class CompletableFutureCounterTest {

private final int counteNumber = 1;
private final int totalCount = 5000;
private final int maxThreadNumber = 15;
private static final Logger logger = LoggerFactory.getLogger(CompletableFutureCounterTest.class);

@Autowired
CompletableFutureCounter counter;
@Test
@DisplayName("CompletableFuture로 스레드 안전한 카운터로 동시에 여러 더하기 수행하기.")
public void 여러_더하기_수행_Executor() throws InterruptedException {
LocalTime lt1 = LocalTime.now();
int initalCount = counter.show();

ExecutorService service = Executors.newFixedThreadPool(maxThreadNumber);
CountDownLatch latch = new CountDownLatch(totalCount);
for (int i = 0; i < totalCount; i++) {
service.submit(() -> {
counter.add(counteNumber);
latch.countDown();
});
}
latch.await();
int finalCount = counter.show();
LocalTime lt2 = LocalTime.now();
long dif = Duration.between(lt1, lt2).getNano();
logger.info("여러_더하기_수행_Executor 테스트가 걸린 시간 : " + ((float)dif / 1000000) + "ms");
Assertions.assertEquals(initalCount + totalCount * counteNumber, finalCount);
}
}
51 changes: 51 additions & 0 deletions src/test/java/com/thread/concurrency/SynchronizedCounterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.thread.concurrency;

import com.thread.concurrency.counter.SynchronizedCounter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.time.LocalTime;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootTest
public class SynchronizedCounterTest {

private final int counteNumber = 1;
private final int totalCount = 5000000;
private final int maxThreadNumber = 15;
private static final Logger logger = LoggerFactory.getLogger(SynchronizedCounterTest.class);

@Autowired
SynchronizedCounter counter;

@Test
@DisplayName("synchronized로 스레드 안전한 카운터로 동시에 여러 더하기 수행하기.")
public void 여러_더하기_수행_Executor() throws InterruptedException {

LocalTime lt1 = LocalTime.now();
int initalCount = counter.show();

ExecutorService service = Executors.newFixedThreadPool(maxThreadNumber);
CountDownLatch latch = new CountDownLatch(totalCount);
for (int i = 0; i < totalCount; i++) {
service.submit(() -> {
counter.add(counteNumber);
latch.countDown();
});
}
latch.await();
int finalCount = counter.show();
LocalTime lt2 = LocalTime.now();
long dif = Duration.between(lt1, lt2).getNano();
logger.info("여러_더하기_수행_Executor 테스트가 걸린 시간 : " + ((float)dif / 1000000) + "ms");
Assertions.assertEquals(initalCount + totalCount * counteNumber, finalCount);
}
}