[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item17 ์™„๋ฒฝ๊ณต๋žต. final๊ณผ ์ž๋ฐ” ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋ธ JMM

2023. 2. 28. 14:03ใ†JAVA/Effective JAVA

728x90
item17. ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์„ ์ตœ์†Œํ™” ํ•˜๋ผ. 

" p105. ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๋ถˆ๋ณ€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋™๊ธฐํ™” ์—†์ด ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ ๊ฑด๋„ค๋„ ๋ฌธ์ œ์—†์ด ๋™์ž‘ํ•œ๋‹ค.(JLS 17.4, 17.5)"

 

 

 

JMM (Java Memory Model) 

: JMM ์ด๋ผ๊ณ  ํ•ด์„œ ๊ตฌ๊ธ€๋งํ•ด๋ณด๋ฉด JVM์˜ ํž™, ์Šคํƒ... ์ด๋Ÿฐ ์„ค๋ช…์œผ๋กœ ์ด์–ด์ง€๋Š” ๊ธ€๋“ค์ด ๋งŽ์€๋ฐ ์ด๊ฑด JMM์œผ๋กœ JVM๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค!! 

์ž๋ฐ” ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋ธ JMM์€ JVM์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์ ๋ฒ•ํ•œ legal ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰๊ทœ์น™์ด๋‹ค. 

https://docs.oracle.com/javase/specs/jls/se14/html/jls-17.html#jls-17.4

 

Chapter 17. Threads and Locks

class A { final int x; A() { x = 1; } int f() { return d(this,this); } int d(A a1, A a2) { int i = a1.x; g(a1); int j = a2.x; return j - i; } static void g(A a) { // uses reflection to change a.x to 2 } } In the d method, the compiler is allowed to reorder

docs.oracle.com

A memory model describes, given a program and an execution trace of that program, whether the execution trace is a legal execution of the program.

The Java programming language memory model works by examining each read in an execution trace and checking that the write observed by that read is valid according to certain rules.

 

The memory model describes possible behaviors of a program. An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.

 

JMM ๊ด€๋ จ ๊ธ€์ด ์ž‘์„ฑ๋œ ๋ธ”๋กœ๊ทธ๋„ ์ฐพ์•„์„œ ์ฒจ๋ถ€~_~ 

https://getchan.github.io/til/java_memory_model/

 

 

 

public class WhiteShip
{
   private  int x;
   private  int y;
   
   public WhiteShip()
   {
      this.x = 1;
      this.y = 2;
   }
   
}

 

 

public static void main(String[] args)
{
   WhiteShip whiteShip = new WhiteShip();
}

๊ทธ๋ƒฅ ๊ฐ„๋‹จํ•˜๊ฒŒ WhiteShip ์ด๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ x๋Š” 1  y๋Š” 2๋กœ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค๊ณ  ํ•  ๋•Œ, 

๋ฉ”์ธ์—์„œ ์ด์— ๋Œ€ํ•œ ๋™์ž‘์„ ์˜ˆ์ธกํ•ด๋ณด๋ฉด 

1. Object o  = new WhiteShip(); 

2. o.x = 1 

3. o.y = 2 

4. whiteship = o ; 

์ด๋Ÿฐ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๊ทธ๋Ÿฐ๋ฐ  JMM์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•˜๋Š” ๋ฒ”์œ„ ๋‚ด์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์–ด๋–ป๊ฒŒ ์‹คํ–‰ํ•˜๋“  ๊ตฌํ˜„์ฒด(JMM)์˜ ์ž์œ ๋‹ค..!! 

๋”ฐ๋ผ์„œ ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•˜๋Š” ๋ฒ”์œ„๋‚ด์—์„œ๋Š” ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€” ์ˆ˜๋„ ์žˆ๋‹ค!!

 

1. Object o  = new WhiteShip();

2. whiteship = o ; 

3. o.x = 1 

4. o.y = 2 

์ด๋Ÿฐ์‹์œผ๋กœ ๋™์ž‘ํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!! 

 

 

https://docs.oracle.com/javase/specs/jls/se14/html/jls-17.html#jls-17.5

 

Chapter 17. Threads and Locks

class A { final int x; A() { x = 1; } int f() { return d(this,this); } int d(A a1, A a2) { int i = a1.x; g(a1); int j = a2.x; return j - i; } static void g(A a) { // uses reflection to change a.x to 2 } } In the d method, the compiler is allowed to reorder

docs.oracle.com

Fields declared final are initialized once, but never changed under normal circumstances. 

 

public class WhiteShip
{
   private final int x;
   private final int y;
   
   public WhiteShip()
   {
      this.x = 1;
      this.y = 2;
   }
   
}

๊ทผ๋ฐ final๋กœ ์„ ์–ธํ•˜๋ฉด final ํ•„๋“œ๋Š” ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด์•ผ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

์–ด๋–ค ์ธ์Šคํ„ด์Šค์˜ final ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ์“ฐ๋ ˆ๋“œ๋Š” ๊ธฐ๋‹ค๋ ค์•ผ ํ•œ๋‹ค(Freeze) 

 

๋”ฐ๋ผ์„œ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋ฐ˜๋“œ์‹œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค๋ฉด final ํ‚ค์›Œ๋“œ๋ฅผ ์จ์•ผํ•œ๋‹ค. 

final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉ์•ˆํ•˜๋ฉด ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ํ•ด๋‹น ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ฐธ์กฐ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

-> ๊ทผ๋ฐ ๊ฐ•์˜๋ฅผ ๋“ฃ๋Š”๋ฐ ๊ฐ•์‚ฌ๋‹˜์ด final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์„œ ์ดˆ๊ธฐํ™” ๋˜์ง€ ์•Š์€ ๊ฐ’์ด ์ฐธ์กฐ๋œ ์ƒํ™ฉ์„ ๊ฒฝํ—˜ํ•ด๋ณธ ์ ์€ ์—†๋‹ค๊ณ  ํ•˜์‹ ๋‹ค! 

ํ•˜์ง€๋งŒ ์ž๋ฐ” ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋ธ์—์„œ๋Š” ๊ทธ๋Ÿด ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค๊ณ  ํ–ˆ์œผ๋‹ˆ๊นŒ ๊ทธ๋Ÿฐ ๊ฐ€๋Šฅ์„ฑ๋„ ์ œ์™ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค~_~ 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90