René's URL Explorer Experiment


Title: 아이템 83. 지연 초기화는 신중히 사용하라 · Study-2-Effective-Java · Discussion #188 · GitHub

Open Graph Title: 아이템 83. 지연 초기화는 신중히 사용하라 · Study-2-Effective-Java · Discussion #188

X Title: 아이템 83. 지연 초기화는 신중히 사용하라 · Study-2-Effective-Java · Discussion #188

Description: 아이템 83. 지연 초기화는 신중히 사용하라

Open Graph Description: 지연 초기화(lazy initialization)란? 필드의 초기화 시점을 그 값이 처음 필요할 때까지 늦추는 기법 주 용도는 크게 1) 최적화, 2) 클래스와 인스턴스 초기화 때 발생하는 위험한 순환 문제 해결 용도로 사용 지연 초기화 == ⚔️ 최선의 조언 "지연 초기화는 필요할 때까지는 하지 말라" feat. #167 클래스 or 인스턴스 생성 ...

X Description: 지연 초기화(lazy initialization)란? 필드의 초기화 시점을 그 값이 처음 필요할 때까지 늦추는 기법 주 용도는 크게 1) 최적화, 2) 클래스와 인스턴스 초기화 때 발생하는 위험한 순환 문제 해결 용도로 사용 지연 초기화 == ⚔️ 최선의 조언 "지연 초기화는 필요할 때까지는 하지 말라" feat. #167 클래스 o...

Opengraph URL: https://github.com/orgs/Study-2-Effective-Java/discussions/188

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"아이템 83. 지연 초기화는 신중히 사용하라","text":"

지연 초기화(lazy initialization)란?

\n
\n

필드의 초기화 시점을 그 값이 처음 필요할 때까지 늦추는 기법
\n주 용도는 크게 1) 최적화, 2) 클래스와 인스턴스 초기화 때 발생하는 위험한 순환 문제 해결 용도로 사용

\n
\n

지연 초기화 == ⚔️

\n

최선의 조언

\n
\n

\"지연 초기화는 필요할 때까지는 하지 말라\"

\n
\n\n

성능을 느리게 할 수도 있는 요인

\n
    \n
  1. 지연 초기화 대상 필드 중 초기화가 이뤄지는 비율에 따라
  2. \n
  3. 실제 초기화에 드는 비용에 따라
  4. \n
  5. 초기화된 각 필드를 얼마나 빈번히 호출하느냐에 따라
  6. \n
\n

그러면 언제 지연 초기화를 쓰라는 거죠?

\n\n

예시

\n

일반적인 초기화 및 인스턴스 필드의 지연 초기화

\n

Nonmember.java

\n
public class Nonmember {\n\n    private final String name;\n\n    public Nonmember(String name) {\n        this.name = name;\n    }\n\n}
\n

Member.java

\n
public class Member {\n\n    private final String name;\n\n    public Member(String name) {\n        this.name = name;\n    }\n\n}
\n

Driven code

\n
public class MemberShell {\n\n    private final Nonmember nonmember = this.createNonmember();\n    private Member member;\n\n    private Nonmember createNonmember() {\n        return new Nonmember(\"Anonymous\");\n    }\n\n    /**\n     * 지연 초기화가 초기화 순환성(initialization circularity)을 해소하기 위해서는 `synchronized` 접근자 메서드 사용\n     */\n    public /* synchronized */ Member getMember() {\n        if (Objects.isNull(member)) {\n            member = this.createMember();\n        }\n\n        return member;\n    }\n\n    private Member createMember() {\n        return new Member(\"corock\");\n    }\n\n}
\n

정적 필드용 지연 초기화

\n\n
public class MemberHolder {\n\n    static final Member member = createMember();\n\n    private static Member createMember() {\n        return new Member(\"CoRock\");\n    }\n\n    public static Member getMember() {\n        return MemberHolder.member;\n    }\n\n}
\n\n

이중검사(double-check) 관용구

\n\n
public class BlueCheck {\n\n    private volatile Member member;\n\n    private static Member createMember() {\n        return new Member(\"CoRock\");\n    }\n\n    private Member getMember() {\n        Member result = member;\n\n        // 첫 번째 검사 (락 사용 안 함)\n        if (Objects.nonNull(result)) {\n            return result;\n        }\n\n        synchronized(this) {\n            // 두 번째 검사 (락 사용)\n            if (Objects.isNull(field)) {\n                member = createMember();\n            }\n        }\n\n        return MemberHolder.member;\n    }\n\n}
\n\n

cf. volatile

\n
\n

자바 변수를 CPU 캐시가 아닌 Main Memory 에 저장한다고 명시하는 것
\n변수 값을 Read & Write 시 Main Memory 로 접근

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\"2\"\"cpu-cache-coherence-and-java-concurrency-1\"
\"VIWlLZq\"\"non-blocking-algorithms-3\"
\n

회고

\n\n

References

\n","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

한 번은 동기화 없이 검사하고, 두 번째는 동기화하여 검사하는 방식으로 필드의 값을 총 두 번 검사
\n-> 이중 검사가 왜 ?? 성능을 높여주지 궁금하여 chatgpt에 물어봤습니다.

\n
지연 초기화에서 한 번은 동기화 없이 필드를 검사하고, 두 번째는 동기화하여 검사하는 방식으로 필드의 값을 총 두 번 검사하는 이유는 성능 향상과 관련이 있습니다.\n\n첫 번째 검사에서는 필드의 값이 이미 초기화되어 있는지 확인합니다. 이 경우에는 두 번째 검사와 동기화를 할 필요가 없기 때문에 불필요한 동기화를 줄일 수 있습니다. 따라서 이로 인해 불필요한 오버헤드가 감소하고, 성능 향상이 이루어집니다.\n\n두 번째 검사에서는 필드의 값이 초기화되어 있지 않은 경우에만 동기화를 하여 필드를 초기화합니다. 이때, 동기화를 통해 다른 스레드가 필드의 값을 수정하는 것을 방지합니다. 또한, 이렇게 초기화된 필드는 이후에는 더 이상 동기화가 필요하지 않기 때문에 불필요한 오버헤드를 줄일 수 있습니다.\n\n이처럼 지연 초기화에서는 필드의 값을 필요한 시점에 초기화하는 방식으로 성능 향상을 이루고 있으며, 이를 위해 불필요한 동기화를 줄이는 것이 중요합니다.\n
","upvoteCount":1,"url":"https://github.com/orgs/Study-2-Effective-Java/discussions/188#discussioncomment-5520809"}}}

route-pattern/_view_fragments/Voltron::DiscussionsFragmentsController/show/orgs/:org/:discussion_number/discussion_layout(.:format)
route-controllervoltron_discussions_fragments
route-actiondiscussion_layout
fetch-noncev2:39c1a654-1a27-2929-eba8-add345d086f8
current-catalog-service-hash9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be
request-idA6D2:B851A:D24EEA:1190E32:696EEA86
html-safe-nonce2b9038a6d0fec623b8b20554ea6bed981f2f5ebe8db0570d481b2947adbdc46f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkQyOkI4NTFBOkQyNEVFQToxMTkwRTMyOjY5NkVFQTg2IiwidmlzaXRvcl9pZCI6IjI2OTQ3MDM3NTM2MjAwMjM5NDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac643d481c41a160283c57c21a264f492f6c9d10de1667fd91d2d046c0002a77c8
hovercard-subject-tagdiscussion:5027608
github-keyboard-shortcutsrepository,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/discussions_fragments/discussion_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/orgs/Study-2-Effective-Java/188/discussion_layout
twitter:imagehttps://opengraph.githubassets.com/10c18f3d766ed55986ab0b106a964eb3335f581b0581bd31c0f3de779311bdaf/orgs/Study-2-Effective-Java/discussions/188
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/10c18f3d766ed55986ab0b106a964eb3335f581b0581bd31c0f3de779311bdaf/orgs/Study-2-Effective-Java/discussions/188
og:image:alt지연 초기화(lazy initialization)란? 필드의 초기화 시점을 그 값이 처음 필요할 때까지 늦추는 기법 주 용도는 크게 1) 최적화, 2) 클래스와 인스턴스 초기화 때 발생하는 위험한 순환 문제 해결 용도로 사용 지연 초기화 == ⚔️ 최선의 조언 "지연 초기화는 필요할 때까지는 하지 말라" feat. #167 클래스 or 인스턴스 생성 ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Noneb278ad162d35332b6de714dfb005de04386c4d92df6475522bef910f491a35ee
turbo-cache-controlno-preview
octolytics-dimension-user_id120388640
octolytics-dimension-user_loginStudy-2-Effective-Java
octolytics-dimension-repository_id577325341
octolytics-dimension-repository_nwoStudy-2-Effective-Java/Effective-Java
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id577325341
octolytics-dimension-repository_network_root_nwoStudy-2-Effective-Java/Effective-Java
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release39aed5006635ab6f45e6b77d23e73b08a00272a3
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Forgs%2FStudy-2-Effective-Java%2Fdiscussions%2F188
Study-2-Effective-Javahttps://patch-diff.githubusercontent.com/Study-2-Effective-Java
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Forgs%2FStudy-2-Effective-Java%2Fdiscussions%2F188
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fdiscussions_fragments%2Fdiscussion_layout&source=header-repo&source_repo=Study-2-Effective-Java%2FEffective-Java
Reloadhttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
Reloadhttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
Reloadhttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
Effective-Java-Study https://patch-diff.githubusercontent.com/Study-2-Effective-Java
Overview https://patch-diff.githubusercontent.com/Study-2-Effective-Java
Repositories https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/repositories
Discussions https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions
Projects https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/projects
Packages https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/packages
People https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/people
Overviewhttps://patch-diff.githubusercontent.com/Study-2-Effective-Java
Repositorieshttps://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/repositories
Discussionshttps://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions
Projectshttps://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/projects
Packageshttps://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/packages
Peoplehttps://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/people
Answered https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#discussioncomment-5520809
JoisFehttps://patch-diff.githubusercontent.com/JoisFe
corock https://patch-diff.githubusercontent.com/corock
3. 과제https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions/categories/3-%EA%B3%BC%EC%A0%9C
아이템 83. 지연 초기화는 신중히 사용하라 https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#top
corock https://patch-diff.githubusercontent.com/corock
Answered https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#discussioncomment-5520809
JoisFehttps://patch-diff.githubusercontent.com/JoisFe
Return to tophttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#top
Please reload this pagehttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
Please reload this pagehttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
corock https://patch-diff.githubusercontent.com/corock
Mar 31, 2023 https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#discussion-5027608
아이템 67. 최적화는 신중히 하라 #167https://github.com/orgs/Study-2-Effective-Java/discussions/167
아이템 79. 과도한 동기화는 피하라 #191https://github.com/orgs/Study-2-Effective-Java/discussions/191
아이템 78. 공유 중인 가변 데이터는 동기화해 사용하라 #182https://github.com/orgs/Study-2-Effective-Java/discussions/182
https://user-images.githubusercontent.com/38161720/229793627-2f25d268-6172-4106-b6b2-cd9fe8b72c23.png
https://camo.githubusercontent.com/12491efb3a3bc5f3ee56734fb35e9d7f40d8baa778d070020a609d71f8e54b57/687474703a2f2f7475746f7269616c732e6a656e6b6f762e636f6d2f696d616765732f6a6176612d636f6e63757272656e63792f6370752d63616368652d636f686572656e63652d616e642d6a6176612d636f6e63757272656e63792d312e706e67
https://user-images.githubusercontent.com/38161720/229793393-1cfb523c-eb25-4c6a-bf74-371c337d0a20.png
https://user-images.githubusercontent.com/38161720/229792619-ee200cf8-c676-4a61-99be-157edd116f28.png
Volatile Vs Atomic [duplicate]https://stackoverflow.com/questions/19744508/volatile-vs-atomic
Volatile Variableshttps://jenkov.com/tutorials/java-concurrency/non-blocking-algorithms.html
Give feedback.https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
JoisFe https://patch-diff.githubusercontent.com/JoisFe
Apr 4, 2023 https://github.com/orgs/Study-2-Effective-Java/discussions/188#discussioncomment-5520809
View full answer https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#discussioncomment-5520809
Oldest https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions/188?sort=old
Newest https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions/188?sort=new
Top https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions/188?sort=top
Please reload this pagehttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
Please reload this pagehttps://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
JoisFe https://patch-diff.githubusercontent.com/JoisFe
Apr 4, 2023 https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188#discussioncomment-5520809
Give feedback.https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
corockhttps://patch-diff.githubusercontent.com/corock
Sign up for freehttps://patch-diff.githubusercontent.com/join?source=comment-repo
Sign in to commenthttps://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Forgs%2FStudy-2-Effective-Java%2Fdiscussions%2F188
📚 3. 과제 https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions/categories/3-%EA%B3%BC%EC%A0%9C
11장 동시성 https://patch-diff.githubusercontent.com/orgs/Study-2-Effective-Java/discussions?discussions_q=label%3A%2211%EC%9E%A5+%EB%8F%99%EC%8B%9C%EC%84%B1%22
https://patch-diff.githubusercontent.com/corock
https://patch-diff.githubusercontent.com/JoisFe
https://patch-diff.githubusercontent.com/Study-2-Effective-Java/Effective-Java/discussions/188
https://patch-diff.githubusercontent.com/settings/replies?return_to=1
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.