ThreadLocalRandom

김승규·2024년 3월 3일

ThreadLocalRandom은 Java 7부터 도입된 클래스로 여러 스레드에서 동시에 랜덤 숫자를 생성할 때 사용한다.

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static void main(String[] args) {
        int randomNum = ThreadLocalRandom.current().nextInt(100, 500);
        System.out.println(randomNum);
    }
}

nextInt(int origin, int bound) : origin(포함) ~ bound(제외)

ThreadLocalRandom은 java.util.Random나 Math.Random과 달리 각 스레드가 독립적으로 작동하여 경쟁하지 않아 성능이 향상된다

profile
꿈꾸는 리얼리스트 개발자 김승규입니다.

0개의 댓글