๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java][Thread] Happens-before relationship

Java์˜ ๋™์‹œ์„ฑ ๊ด€๋ จ ๋ถ€๋ถ„์„ ์‚ดํ”ผ๋‹ค๊ฐ€ 'happens-before relationship'์ด๋ž€ ์šฉ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค.

 

https://www.logicbig.com/tutorials/core-java-tutorial/java-multi-threading/happens-before.html

 

Java - Understanding Happens-before relationship

Java - Understanding Happens-before relationship [Last Updated: May 19, 2018]

www.logicbig.com

https://en.wikipedia.org/wiki/Happened-before

 

Happened-before - Wikipedia

From Wikipedia, the free encyclopedia In computer science, the happened-before relation (denoted: → {\displaystyle \to \;} ) is a relation between the result of two events, such that if one event should happen before another event, the result must reflec

en.wikipedia.org

https://preshing.com/20130702/the-happens-before-relation/

 

The Happens-Before Relation

Happens-before is a modern computer science term which is instrumental in describing the software memory models behind C++11, Java, Go and even LLVM. You’ll find a definition of the happens-before relation in the specifications of each of the above langu

preshing.com

https://jenkov.com/tutorials/java-concurrency/java-happens-before-guarantee.html

 

https://javarevisited.blogspot.com/2020/01/what-is-happens-before-in-java-concurrency.html

 

What is happens-before in Java Concurrency? An example

A blog about Java, Programming, Algorithms, Data Structure, SQL, Linux, Database, Interview questions, and my personal experience.

javarevisited.blogspot.com

https://www.geeksforgeeks.org/happens-before-relationship-in-java/

 

Happens-Before Relationship in Java - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

 

์œ„ํ‚คํ”ผ๋””์•„์˜ ์„ค๋ช…. happens-before๊ฐ€ ์•„๋‹ˆ๋ผ happened-before๋กœ ์šฉ์–ด ์„ค๋ช…์„ ํ•˜๊ณ  ์žˆ๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ๋‘˜์˜ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

In computer science, the happened-before relation (denoted: →) is a relation between the result of two events, such that if one event should happen before another event, the result must reflect that, even if those events are in reality executed out of order (usually to optimize program flow).

๋ฐœ๋ฒˆ์—ญ

์ปดํ“จํ„ฐ ๊ณผํ•™์—์„œ, happened-before ๊ด€๊ณ„๋Š” ๋‘ ์ด๋ฒคํŠธ์˜ ๊ฒฐ๊ณผ๋“ค ์‚ฌ์ด์˜ ๊ด€๊ณ„์ด๋‹ค. ๋งŒ์•ฝ ํ•œ ์ด๋ฒคํŠธ๊ฐ€ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ ์ „์— ์ผ์–ด๋‚˜์•ผ ํ•œ๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ๊ทธ๊ฒƒ์„ ๋ฐ˜์˜ํ•ด์•ผ ํ•œ๋‹ค. ์‹ค์ œ๋กœ ๊ทธ ์ด๋ฒคํŠธ๋“ค์ด (์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด) ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋˜์ง€ ์•Š๋”๋ผ๋„ ๋ง์ด๋‹ค.

 

๋‚ด๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”๋Š” ์ด๋ ‡๋‹ค.

์ด๋ฒคํŠธ a์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ์ด๋ฒคํŠธ b์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด์•ผ ํ•  ๊ฒฝ์šฐ > a  happenes-before b.

์“ฐ๋ ˆ๋“œ 1์—์„œ ์ด๋ฒคํŠธ a, ์“ฐ๋ ˆ๋“œ 2์—์„œ ์ด๋ฒคํŠธ b๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๋ฉด, ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ a ๋‹ค์Œ b์ผ์ง€, b ๋‹ค์Œ a ์ผ์ง€๋Š” ๋ชจ๋ฅด์ง€๋งŒ, ๊ฒฐ๊ณผ๋Š” a์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  b์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด์•ผ ํ•œ๋‹ค.

Happens-before ์šฉ์–ด๋Š” Java๋งŒ์˜ ์šฉ์–ด๋Š” ์•„๋‹ˆ๋‹ค. happenes-before relation์„ ํ†ตํ•ด ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ, ๋ฉ€ํ‹ฐ ์ฝ”์–ด ํ™˜๊ฒฝ์—์„œ์˜ ์„ฑ๋Šฅ์€ ํ–ฅ์ƒ(๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ-๋™์‹œ์„ฑ, ๋ฉ€ํ‹ฐ ์ฝ”์–ด-๋ณ‘๋ ฌ์„ฑ)์‹œํ‚ค๋ฉด์„œ ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‚˜์˜ค๋„๋ก ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

Java์—์„œ Happens-before relationship์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ• 

  • Synchronized ์‚ฌ์šฉ
  • Volatile ์‚ฌ์šฉ