Skip to Content
Java의 현대적 동시성
book

Java의 현대적 동시성

by A N M Bazlur Rahman
September 2025
Beginner to intermediate
336 pages
5h 57m
Korean
O'Reilly Media, Inc.
Content preview from Java의 현대적 동시성

5장. 범위 지정 값

이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com

물건을 제자리에 두지 않는 사람은 불의를 저지른 것입니다.

알리 이븐 아비 탈립(하나님이 그를 기뻐하시길)

이 장에서는 JDK 25에서 완성된 강력한 Java 추가 기능인 ScopedValue 에 대해 살펴봅니다. 이는 접근 가능하고 컨텍스트와 일관성을 유지하면서 값을 특정 범위에 바인딩하는 구조화된 방법을 제공합니다. 번거롭고 메모리 누수가 발생하기 쉬운 기존의 Thread​Local 변수와 달리, 멀티스레드 애플리케이션을 처리할 때 보다 깔끔하고 효율적인 접근 방식을 제공합니다. 이 장에서는 Thread​Local 대신 ScopedValue 및 해당 API를 사용하는 것이 더 나은 이유를 살펴본 다음 실제 사용 사례를 살펴보겠습니다.

시작하겠습니다.

컨텍스트 전달의 부담

단순히 메서드 인수를 사용하는 것이 불가능한 코드의 여러 부분 간에 데이터를 공유해야 하는 시나리오가 종종 있습니다. 애플리케이션이 프레임워크에 의존하는 경우 특히 그렇습니다. 이 개념을 설명하기 위해 한 가지 예를 들어보겠습니다.

사용자 코드가 프레임워크가 실행할 작업을 등록하는 작업 스케줄링 프레임워크를 상상해 보겠습니다. 이 프레임워크는 작업을 실행할 때마다 작업의 이름, 우선순위, 스케줄링 제약 조건 등의 메타데이터가 포함된 JobContext 개체를 만듭니다. 이 컨텍스트는 프레임워크 작업에 필수적이지만 사용자 코드와는 크게 관련이 없습니다.

다음 예는 "매개변수 전달 문제"라고 부르는 일반적인 작업 스케줄링 프레임워크를 보여줍니다:

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

interface Job {
    void execute(JobContext context);
}

enum Priority { LOW, MEDIUM, HIGH }

public record JobContext(String jobName, Priority priority,
                         Map<String, Object> metadata) {
    public JobContext(String jobName, Priority priority) {
        this(jobName, priority, new HashMap<>());
        metadata.put("jobName", jobName);
        metadata.put("priority", priority);
        metadata.put("creationTime", Instant.now());
    }

    public Object getMetadataValue(String key) {
        return metadata.get(key);
    }
}

프레임워크의 핵심 스케줄링 로직은 작업 컨텍스트를 생성하고 관리합니다:

// Framework code
public class JobScheduler {
    public void schedule
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

머신러닝 시스템 설계

머신러닝 시스템 설계

칩 후옌
이펙티브 러스트

이펙티브 러스트

데이비드 드라이스데일
자바 개발자를 위한 데브옵스 툴

자바 개발자를 위한 데브옵스 툴

스티븐 친, 멜리사 맥케이, 익스헬 루이츠, 바루크 사도구르스키

Publisher Resources

ISBN: 9798341670174