CodeGym /Java Course /๋ชจ๋“ˆ 2: ์ž๋ฐ” ์ฝ”์–ด /์Šค๋ ˆ๋“œ๋Š” ๋…๋ฆฝ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๋™๊ธฐํ™”!

์Šค๋ ˆ๋“œ๋Š” ๋…๋ฆฝ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๋™๊ธฐํ™”!

๋ชจ๋“ˆ 2: ์ž๋ฐ” ์ฝ”์–ด
๋ ˆ๋ฒจ 12 , ๋ ˆ์Šจ 1
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•ˆ๋…•ํ•˜์„ธ์š”, ์•„๋ฏธ๊ณ ! ๋งŒ๋ณ‘ํ†ต์น˜์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ๋ณ‘ํ†ต์น˜์•ฝ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ ์‚ดํŽด๋ณด์•˜๋“ฏ์ด ์ œ์–ด๋˜์ง€ ์•Š์€ ์Šค๋ ˆ๋“œ ์ „ํ™˜์ด ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค."

"์Šค๋ ˆ๋“œ๊ฐ€ ๋‹ค์Œ ์Šค๋ ˆ๋“œ๋กœ ์ „ํ™˜ํ•  ์‹œ๊ธฐ๋ฅผ ์Šค์Šค๋กœ ๊ฒฐ์ •ํ•  ์ˆ˜ ์—†๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ํ•„์š”ํ•œ ๋ชจ๋“  ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ ยซ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!ยป๋ผ๊ณ  ์‹ ํ˜ธ๋ฅผ ๋ณด๋‚ด์•ผ ํ•ฉ๋‹ˆ๊นŒ?"

"์Šค๋ ˆ๋“œ ์ž์ฒด๊ฐ€ ์ „ํ™˜์„ ์ œ์–ดํ•˜๋„๋ก ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ›จ์”ฌ ๋” ํฐ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž˜๋ชป ์ž‘์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์žˆ๊ณ  ์Šค๋ ˆ๋“œ๊ฐ€ CPU๋ฅผ ํฌ๊ธฐํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ์—๋Š” ์ด๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์€ ๊ฝค ์•…๋ชฝ์ด์—ˆ์Šต๋‹ˆ๋‹ค."

"์•Œ์•˜์–ด. ๊ทธ๋ž˜์„œ ํ•ด๊ฒฐ์ฑ…์ด ๋ญ์•ผ?"

" ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จํ•ฉ๋‹ˆ๋‹ค.  ์ด๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค."

์Šค๋ ˆ๋“œ๊ฐ€ ๊ณต์œ  ๊ฐœ์ฒด ๋ฐ/๋˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์Šค๋ ˆ๋“œ๊ฐ€ ์„œ๋กœ ๊ฐ„์„ญํ•œ๋‹ค๋Š” ๊ฒƒ์ด ๋ถ„๋ช…ํ•ด์กŒ์Šต๋‹ˆ๋‹ค . ์ฝ˜์†” ์ถœ๋ ฅ์˜ ์˜ˆ์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ์ฝ˜์†”๊ณผ ์—ฌ๊ธฐ์— ๋Œ€ํ•œ ๋ชจ๋“  ์Šค๋ ˆ๋“œ ์ถœ๋ ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€์ €๋ถ„ํ•ด.

๊ทธ๋ž˜์„œ ํŠน๋ณ„ํ•œ ๊ฐ์ฒด์ธ ๋ฎคํ…์Šค ๊ฐ€ ๋ฐœ๋ช…๋˜์—ˆ์Šต๋‹ˆ๋‹ค . ํ™”์žฅ์‹ค ๋ฌธ์— ยซ์‚ฌ์šฉ ๊ฐ€๋Šฅ / ์ ์œ ยป ๋ผ๊ณ  ํ‘œ์‹œ๋œ ํ‘œ์ง€ํŒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค . ๋‘ ๊ฐ€์ง€ ์ƒํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: ๊ฐœ์ฒด๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ ์ค‘ ์ž…๋‹ˆ๋‹ค . ์ด๋Ÿฌํ•œ ์ƒํƒœ๋ฅผ ยซ์ž ๊น€ยป ๋ฐ ยซ์ž ๊ธˆ ํ•ด์ œยป๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์Šค๋ ˆ๋“œ๊ฐ€ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์™€ ๊ณต์œ ํ•˜๋Š” ๊ฐœ์ฒด๊ฐ€ ํ•„์š”ํ•  ๋•Œ ๊ฐœ์ฒด์™€ ๊ด€๋ จ๋œ ๋ฎคํ…์Šค๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ฎคํ…์Šค๊ฐ€ ์ž ๊ธˆ ํ•ด์ œ๋˜๋ฉด ์Šค๋ ˆ๋“œ๋Š” ์ด๋ฅผ ์ž ๊ทธ๊ณ (ยซ์‚ฌ์šฉ๋จยป์œผ๋กœ ํ‘œ์‹œ) ๊ณต์œ  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์Šค๋ ˆ๋“œ๊ฐ€ ์ž‘์—…์„ ์™„๋ฃŒํ•œ ํ›„ ๋ฎคํ…์Šค๊ฐ€ ์ž ๊ธˆ ํ•ด์ œ๋ฉ๋‹ˆ๋‹ค(ยซ์‚ฌ์šฉ ๊ฐ€๋Šฅยป์œผ๋กœ ํ‘œ์‹œ๋จ).

์Šค๋ ˆ๋“œ๊ฐ€ ๊ฐœ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ณ  ๋ฎคํ…์Šค๊ฐ€ ์ž ๊ธด ๊ฒฝ์šฐ ์Šค๋ ˆ๋“œ๋Š” ๋Œ€๊ธฐํ•˜๋Š” ๋™์•ˆ ํœด๋ฉด ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฎคํ…์Šค๊ฐ€ ์ ์œ  ์Šค๋ ˆ๋“œ์— ์˜ํ•ด ์ตœ์ข…์ ์œผ๋กœ ์ž ๊ธˆ ํ•ด์ œ๋˜๋ฉด ์Šค๋ ˆ๋“œ๋Š” ์ฆ‰์‹œ ์ž ๊ทธ๊ณ  ์‹คํ–‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์š•์‹ค ๋„์–ด ์‚ฌ์ธ๊ณผ์˜ ๋น„์œ ๋Š” ์™„๋ฒฝํ•ฉ๋‹ˆ๋‹ค.

"๊ทธ๋Ÿผ ๋ฎคํ…์Šค๋กœ ์ž‘์—…ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ? ํŠน์ˆ˜ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๊นŒ?"

"๊ทธ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. Java ์ œ์ž‘์ž๋Š” ์ด ๋ฎคํ…์Šค๋ฅผ Object ํด๋ž˜์Šค์— ๋‚ด์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งŒ๋“ค ํ•„์š”์กฐ์ฐจ ์—†์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฐ์ฒด์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค. ์ž‘๋™ ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค."

์•”ํ˜ธ ์„ค๋ช…
class MyClass
{
private String name1 = "Ally";
private String name2 = "Lena";

public void swap()
{
synchronized (this)
{
String s = name1;
name1 = name2;
name2 = s;
}
}
}
swap ๋ฉ”์†Œ๋“œ๋Š” name1 ๋ฐ name2 ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ตํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋™์‹œ์— ๋‘ ์Šค๋ ˆ๋“œ์—์„œ ํ˜ธ์ถœ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

์‹ค์ œ ์ฝ”๋“œ ์‹คํ–‰ ์ฒซ ๋ฒˆ์งธ ์Šค๋ ˆ๋“œ์˜ ์ฝ”๋“œ ๋‘ ๋ฒˆ์งธ ์Šค๋ ˆ๋“œ์˜ ์ฝ”๋“œ
String s1 = name1; //Ally
name1 = name2; //Lena
name2 = s1; //Ally

String s2 = name1; //Lena
name1 = name2; //Ally
name2 = s2; //Lena
String s1 = name1;
name1 = name2;
//other thread is running
name2 = s1;
//the thread waits until the mutex is unlocked

String s2 = name1;
name1 = name2;
//other thread is running
//other thread is running
name2 = s2;
๊ฒฐ๋ก 
๋ณ€์ˆ˜ ๊ฐ’์ด ๋‘ ๋ฒˆ ๋ฐ”๋€Œ์–ด ์›๋ž˜ ์œ„์น˜๋กœ ๋Œ์•„๊ฐ”์Šต๋‹ˆ๋‹ค.

๋™๊ธฐํ™”๋œ ํ‚ค์›Œ๋“œ์— ์ฃผ์˜ํ•˜์‹ญ์‹œ์˜ค  .

"๊ทธ๋ž˜, ๊ทธ๊ฒŒ ๋ฌด์Šจ ๋œป์ด์•ผ?"

"์Šค๋ ˆ๋“œ๊ฐ€ ๋™๊ธฐํ™”๋จ์œผ๋กœ ํ‘œ์‹œ๋œ ์ฝ”๋“œ ๋ธ”๋ก์— ๋“ค์–ด๊ฐ€๋ฉด Java ์‹œ์Šคํ…œ์€ ๋™๊ธฐํ™”๋ผ๋Š” ๋‹จ์–ด ๋’ค์— ๊ด„ํ˜ธ ์•ˆ์— ํ‘œ์‹œ๋œ ๊ฐ์ฒด์˜ ๋ฎคํ…์Šค๋ฅผ ์ฆ‰์‹œ ์ž ๊ธ‰๋‹ˆ๋‹ค. ์Šค๋ ˆ๋“œ๊ฐ€ ๋– ๋‚  ๋•Œ๊นŒ์ง€ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋Š” ์ด ๋ธ”๋ก์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์Šค๋ ˆ๋“œ๊ฐ€ ๋– ๋‚˜๋Š” ์ฆ‰์‹œ ๋™๊ธฐํ™”๋œ ๊ฒƒ์œผ๋กœ ํ‘œ์‹œ๋œ ๋ธ”๋ก, ๋ฎคํ…์Šค๋Š” ์ฆ‰์‹œ ์ž๋™์œผ๋กœ ์ž ๊ธˆ ํ•ด์ œ๋˜๋ฉฐ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ํš๋“ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

๋ฎคํ…์Šค๊ฐ€ ์‚ฌ์šฉ ์ค‘์ด๋ฉด ์Šค๋ ˆ๋“œ๋Š” ๊ฐ€๋งŒํžˆ ์„œ์„œ ํ•ด์ œ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.

"๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ณ  ์šฐ์•„ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ฆ„๋‹ค์šด ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค."

"์˜ˆ. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๊นŒ?"

์•”ํ˜ธ ์„ค๋ช…
class MyClass
{
private String name1 = "Ally";
private String name2 = "Lena";

public void swap()
{
synchronized (this)
{
String s = name1;
name1 = name2;
name2 = s;
}
}

public void swap2()
{
synchronized (this)
{
String s = name1;
name1 = name2;
name2 = s;
}
}
}
swap ๋ฐ swap2 ๋ฉ”์„œ๋“œ๋Š” ๋™์ผํ•œ ๋ฎคํ…์Šค ( this ๊ฐœ์ฒด)๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.

ํ•œ ์Šค๋ ˆ๋“œ๊ฐ€ swap ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๊ฐ€ swap2 ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ฉ๋‹ˆ๊นŒ?

"๋ฎคํ…์Šค๊ฐ€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ฒˆ์งธ ์Šค๋ ˆ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™๊ธฐํ™”๋œ ๋ธ”๋ก์„ ๋– ๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋™์‹œ ์•ก์„ธ์Šค์— ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค."

"์ž˜ํ–ˆ์–ด, ์•„๋ฏธ๊ณ ! ์ •๋‹ต์ด์•ผ!"

์ด์ œ ๋‚˜๋Š” synchronized๊ฐ€ ์ฝ”๋“œ ๋ธ”๋ก๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฉ”์†Œ๋“œ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Œ์„ ์ง€์ ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค . ์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์•”ํ˜ธ ์‹ค์ œ๋กœ ์ผ์–ด๋‚˜๋Š” ์ผ
class MyClass
{
private static String name1 = "Ally";
private static String name2 = "Lena";

public synchronized void swap()
{
String s = name1;
name1 = name2;
name2 = s;
}

public static synchronized void swap2()
{
String s = name1;
name1 = name2;
name2 = s;
}
}
class MyClass
{
private static String name1 = "Ally";
private static String name2 = "Lena";

public void swap()
{
synchronized (this)
{
String s = name1;
name1 = name2;
name2 = s;
}
}

public static void swap2()
{
synchronized (MyClass.class)
{
String s = name1;
name1 = name2;
name2 = s;
}
}
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION