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

Feature: 유저 프로필 (기본, 소개글) 생성/수정 (#PC-80) #7

Merged
merged 15 commits into from
Dec 26, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.yapp.domain.profile.application.util;

import java.util.List;
import java.util.Random;

public class NickNameGenerator {
// 한글 동물 이름 10가지
private static final List<String> ANIMAL_NAMES =
List.of("사자", "호랑이", "곰", "독수리", "늑대", "판다", "여우", "사슴", "토끼", "돌고래");

private static final Random RANDOM = new Random();

public static String generateNickname() {
String animal = ANIMAL_NAMES.get(RANDOM.nextInt(ANIMAL_NAMES.size()));
int number = RANDOM.nextInt(10000) + 1;
return animal + number;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바 공식문서에 따르면, java.util.Random을 멀티스레드에서 동시에 이용할 때 성능 저하를 보일 수 있다고 합니다.
ThreadLocalRandom을 고려해보는 것은 어떨까요?

아래는 오라클 공식문서의 설명입니다!

the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAVA의 Radmom은 시드 값을 기반으로 난수 값을 생성한다고 합니다. 그래서 시드 값이 같으면 같은 난수를 생성하며, 다르면 동일하지 않은 값을 생성합니다. 그래서, Random은 시드 값으로 난수를 생성하고 나면 시드를 업데이트 해야하는데, 여러 개의 쓰레드에서 동시에 업데이트하려고 할 때, 순서를 보장하기 위해 CAS연산을 진행하며 이떄 성능저하가 발생하는 것 같습니다.

ThreadLocalRandom은 Radmon이 여러 쓰레드에게 공유되어 발생했던 문제를, 각 스레드마다 고유한 난수 생성기와 시드를 할당 함으로써 해결한다고 합니다. 그리고 ThreadLocal이란 방식으로 Random을 각 쓰레드에게 배정해주는 방법도 있는 거 같은데, ThreadLcoalRandom 사용법이 더 간단하고 효율적인 것 같습니다.

ThreadLcoalRandom로 수정하겠습니다 ~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자세한 정보 감사합니다!
고생하셨습니다.

}