2. Bank Account Problem(์€ํ–‰ ๊ณ„์ขŒ ๋ฌธ์ œ)

๋™๊ธฐํ™”์˜ ํ•„์š”์„ฑ์„ ๊ฐ€์žฅ ์ž˜ ๋Š๋‚„ ์ˆ˜ ์žˆ๋Š” ์€ํ–‰ ๊ณ„์ขŒ ๋ฌธ์ œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค.

2.1 ๋ฌธ์ œ ์„ค๋ช…

๋™๊ธฐํ™” ๋ฌธ์ œ ์ค‘์—์„œ ๋Œ€ํ‘œ์ ์ธ ์€ํ–‰ ๊ณ„์ขŒ ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž. ์€ํ–‰์—๋Š” ํ•˜๋‚˜์˜ ๊ณ„์ขŒ์— ์ž…๊ธˆ, ์ถœ๊ธˆ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณ„์ขŒ๋Š” ๊ณต์œ ํ•˜๋Š” ์ž์›์ด๊ณ , ์ž…๊ธˆ๊ณผ ์ถœ๊ธˆ์€ ๊ฐ๊ฐ ํ”„๋กœ์„ธ์Šค๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ถ€๋ชจ๋‹˜์ด ์ž์‹์—๊ฒŒ ์ž…๊ธˆ์„ ํ•˜๊ณ , ์ž์‹์€ ์ถœ๊ธˆ์„ ํ•˜๋Š” ์ƒํ™ฉ์„ ์ž๋ฐ”๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

// ๊ณ„์ขŒ
class BankAccount {
	int balance;
	void deposit(int amount) {
		balance = balance + amount;
	}
	void withdraw(int amount) {
		balance = balance - amount;
	}
	int getBalance() {
		return balance;
	}
}

์ž…๊ธˆ, ์ถœ๊ธˆ, ์ž”์•ก์กฐํšŒ ํ•จ์ˆ˜๋ฅผ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋กœ ๊ฐ–๋Š” ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

// ์ž…๊ธˆ ํ”„๋กœ์„ธ์Šค
class Parent extends Thread {
	BankAccount b;
  // ์ƒ์„ฑ์ž๋Š” ๊ณต์œ ํ•˜๋Š” ๊ณ„์ขŒ๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๊ฐ€์ง„๋‹ค.
	Parent(BankAccount b) {
		this.b = b;
	}
	public void run() {   // run(): ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹ค์ œ๋กœ ๋™์ž‘ํ•˜๋Š” ๋ถ€๋ถ„(์น˜ํ™˜)
		for (int i = 0; i < 100; i++)
		  b.deposit(1000);
	}
}

๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ๊ณ„์ขŒ๋ฅผ ๊ฐ€์ง€๊ณ , ์ด๊ฒƒ์„ ์ƒ์„ฑํ•  ๋•Œ ์ธ์ž๋กœ ๋ฐ›๋Š” ํด๋ž˜์Šค์ด๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ run()์€ ์ž…๊ธˆ์„ 100๋ฒˆ ์ˆ˜ํ–‰ํ•œ๋‹ค.

// ์ถœ๊ธˆ ํ”„๋กœ์„ธ์Šค
class Child extends Thread {
	BankAccount b;
  // ์ƒ์„ฑ์ž๋Š” ๊ณต์œ ํ•˜๋Š” ๊ณ„์ขŒ๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๊ฐ€์ง„๋‹ค.
	Child(BankAccount b) {
		this.b = b;
	}
	public void run() {
		for (int i = 0; i < 100; i++)
		  b.withdraw(1000);
	}
}

๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ๊ณ„์ขŒ๋ฅผ ๊ฐ€์ง€๊ณ , ์ด๊ฒƒ์„ ์ƒ์„ฑํ•  ๋•Œ ์ธ์ž๋กœ ๋ฐ›๋Š” ํด๋ž˜์Šค์ด๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ run()์€ ์ถœ๊ธˆ์„ 100๋ฒˆ ์ˆ˜ํ–‰ํ•œ๋‹ค.

// Test.java
class Test {
	public static void main(String[] args) throws InterruptedException {
		BankAccount b = new BankAccount();
		Parent p = new Parent(b);
		Child c = new Child(b);
		p.start();   // start(): ์“ฐ๋ ˆ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
		c.start();
		p.join();    // join(): ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋๋‚˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
		c.join();
		System.out.println("balance = " + b.getBalance());
	}
}

main ํ•จ์ˆ˜์—์„œ๋Š” ์œ„์—์„œ ๋งŒ๋“  ํด๋ž˜์Šค๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ, ๊ณ„์ขŒ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ถ€๋ชจ, ์ž์‹ ํด๋ž˜์Šค์—์„œ ๊ฐ๊ฐ ์ž…๊ธˆ๊ณผ ์ถœ๊ธˆ์„ 100๋ฒˆ์”ฉ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‚จ์€ ์ž”์•ก์„ ์กฐํšŒํ•œ๋‹ค. ์ž”์•ก์€ 0์ด ๋‚˜์™€์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

// Result
balance = 0

์ด ๊ฒฐ๊ณผ๋Š” ์ •์ƒ์ ์ด๋‹ค. 100๋ฒˆ 1,000์›์„ ์ž…๊ธˆํ•˜๊ณ , 100๋ฒˆ 1,000์›์„ ์ถœ๊ธˆํ•˜๋ฉด ์ž”์•ก์€ 0์›์ด ๋‚จ๋Š”๋‹ค. ์œ„ ์ฝ”๋“œ๋Š” 2๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์ž‘ํ•˜๊ณ  ์žˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋™๊ธฐํ™” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ํ™•๋ฅ ์€ ๋งค์šฐ ๋‚ฎ๋‹ค. ๋ฐ˜๋ณต์ด 100๋ฒˆ ๋ฐ–์— ์•ˆ ์ผ์–ด๋‚˜๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

2.2 ๋™๊ธฐํ™” ๋ฌธ์ œ ๋ฐœ์ƒ

์‹ค์ œ ์ƒํ™ฉ์—์„œ๋Š”, ์ž…๊ธˆ, ์ถœ๊ธˆ ๋ช…๋ น์„ ๋‚ด๋ฆฌ๊ณ  ์ด ๋ช…๋ น์ด ์ „๋‹ฌ๋˜๋Š”๋ฐ ๊นŒ์ง€ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค. ๊ทธ๋Ÿฐ ์ƒํ™ฉ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ถœ๊ธˆํ•˜๋Š” ๊ณผ์ •์—์„œ temp๋ผ๋Š” ํ•„์š”์—†๋Š” ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , +,-๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ 1000๋ฒˆ์œผ๋กœ ๋Š˜๋ ธ๋‹ค.

// ๊ณ„์ขŒ
class BankAccount {
	int balance;
	void deposit(int amount) {
		int temp = balance + amount;
		System.out.print("+");
		balance = temp;
	}
	void withdraw(int amount) {
		int temp = balance - amount;
		System.out.print("-");
		balance = temp;
	}
	int getBalance() {
		return balance;
	}
}
// Result
++++++++++++++++++++++++++++++++++----------------------------------------------
--------------------------------------------------------------------------++++++
+++----------------------------------------------+++++++++++++++++++++++++++++++
+----+++++++-+++++----+++-------------------------------------------------------
-+++++++-++++-+++++++++-------++++++++++++++++++++++++++++++++++++++++++++++++++
++++++---------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+-++++++++++++-------------------++++++++++++++++++++-++++++++++++++++++++++++++
++++++-+------------------------------------------------------------------------
-+++++++++++-+++++++----------------------------------------+-------+-----------
-+------+-----------------------------------------------------------------------
-+------------------------------------------------------------------------------
-+------------------------------------------------------------------------------
-------------------+-------+----------------------------------------------------
------------------------------+-------------------------------------------------
------------------------------------------------------+-------------------------
-+------------------------------------------------------------------------------
-++---------------------------------------++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
balance = 1000000

+๋Š” ์ž…๊ธˆ์„ ํ•œ ๊ฒฝ์šฐ, -๋Š” ์ถœ๊ธˆ์„ ํ•œ ๊ฒฝ์šฐ์ด๊ณ , ์ฃผ๋ชฉํ•  ์ ์€ balance๊ฐ’์ด 0์ด ์•„๋‹Œ 1000000์ด๋ผ๋Š” ์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๋˜ํ•œ ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ 100000์ด๋ผ๋Š” ๊ฐ’์ด ๊ณ ์ •๋˜์–ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ ๋ถˆ์ผ์น˜ํ•˜๋ฉฐ, ๋น„์ผ๊ด€์ ์ด๋‹ค. (๋น„์ผ๊ด€์ ์ธ ์ด์œ ๋Š” ์šด์˜์ฒด์ œ์—์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ์Šค์œ„์นญํ•˜๋Š” ํŒจํ„ด์ด ๋งค๋ฒˆ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.)

์•ฝ๊ฐ„์˜ ์‹œ๊ฐ„ ์ง€์—ฐ์„ ์ค€ ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ ๊ณต์œ  ์ž์›์„ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์€ ๋ง๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

2.3 ์™œ ๋ฐœ์ƒํ• ๊นŒ?

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์›์ธ์€ ๊ณตํ†ต๋ณ€์ˆ˜(common variable)์— ๋Œ€ํ•œ ๋™์‹œ ์—…๋ฐ์ดํŠธ(concurrent update) ๋•Œ๋ฌธ์ด๋‹ค.

์œ„ ์˜ˆ์ œ์—์„œ ๊ณตํ†ต ๋ณ€์ˆ˜๋Š” ๊ณ„์ขŒ์˜ ์ž”์•ก์ด๋‹ค. ์ด์— ์ ‘๊ทผํ•˜๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

balance = balance + amount;   // ์ž…๊ธˆ
balance = balance - amount;   // ์ถœ๊ธˆ

์ด๋Š” ์ž๋ฐ” ๋ฌธ๋ฒ•์—์„œ๋Š” ํ•œ ์ค„์ด๋ผ ๋ฌธ์ œ๊ฐ€ ์—†์–ด ๋ณด์ด์ง€๋งŒ, ๋กœ์šฐ ๋ ˆ๋ฒจ(์–ด์…ˆ๋ธ”๋ฆฌ์–ด)๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด ์—ฌ๋Ÿฌ ์ค„๋กœ ๊ตฌํ˜„๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, balance๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ํ•˜์ด ๋ ˆ๋ฒจ ์ฝ”๋“œ 1์ค„์ด ๋กœ์šฐ ๋ ˆ๋ฒจ์—์„œ 3์ค„๋กœ ๊ตฌํ˜„๋œ๋‹ค๊ณ  ํ•˜์ž.

  1. ๊ณต์œ  ๋ณ€์ˆ˜์ธ ํ˜„์žฌ ์ž”์•ก์„ ๋ณต์‚ฌํ•œ๋‹ค.
  2. ํ˜„์žฌ ์ž”์•ก์—์„œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  3. ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ์ž”์•ก์„ ๊ฐฑ์‹ ํ•œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, parent๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋˜ ๋„์ค‘์— 1000๋ฒˆ์„ ๋‹ค ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ•œ ์ƒํ™ฉ์—์„œ interrupt(์ด ๊ฒฝ์šฐ์—๋Š” ์‹œ๊ฐ„ ์ง€์—ฐ์„ ๊ฑธ์—ˆ์œผ๋ฏ€๋กœ timer interrupt๊ฐ€ ๋˜๊ฒ ๋‹ค.)๊ฐ€ ๊ฑธ๋ ค 2๋ฒˆ ๋ผ์ธ์—์„œ ๋ฉˆ์ท„๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. parent๊ฐ€ 1000์›์„ ์ž…๊ธˆํ–ˆ์ง€๋งŒ, ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์ง€ ์•Š์•„ ์—ฌ์ „ํžˆ ํ˜„์žฌ ์ž”์•ก์€ 0์ด๋‹ค.

์ด๋ฒˆ์—๋Š” child๊ฐ€ ๋ถ€๋ชจ๋‹˜์ด ์šฉ๋ˆ์„ ๋„ฃ์€ ์ค„ ์•Œ๊ณ  ์‹ ๋‚˜์„œ ๋ˆ์„ ๋ฝ‘๋Š” ์ƒํ™ฉ์„ ์ƒ๊ฐํ•ด๋ณด์ž. ์ž”์•ก์„ ํ™•์ธํ–ˆ๋”๋‹ˆ, ์•„์ง 0์ด๋‹ค. ๊ทธ๋ž˜์„œ child๋Š” ๋งˆ์ด๋„ˆ์Šค ํ†ต์žฅ์„ ํ†ตํ•ด ๋ˆ์€ ์ธ์ถœํ•˜๊ณ  ํ˜„์žฌ ์ž”์•ก์€ -1000์ด ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์ด ๊ฒฝ์šฐ, ์ž…๊ธˆ๊ณผ ์ธ์ถœ์ด๋ผ๋Š” ํ–‰์œ„๋Š” ํ”„๋กœ์„ธ์Šค ์ž…์žฅ์—์„œ ์›์ž์„ฑ์„ ๊ฐ–๋Š” ํ–‰์œ„์ด๋ฏ€๋กœ ๋™๊ธฐํ™”๊ฐ€ ํ•„์ˆ˜์ ์ด๋‹ค. ๊ธฐ์–ต์ด ์•ˆ๋‚œ๋‹ค๋ฉด Introduction์„ ๋ณด์ž.

Reference