10.1. 쓰레드(Thread)
10.1.1. 프로세스(Process)와 쓰레드(Thread)
- 프로세스(Process) : 실행중인 프로그램으로 리소스(resources)와 쓰레드(thread)로 구성
- 쓰레드(Thread) : 프로세스 내에서 실제 작업을 수행
- 멀티 쓰레드 : 하나의 프로세스 내에서 하나 이상의 쓰레드로 구성
10.1.2. 싱글 쓰레드와 멀티 쓰레드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class ThreadClass extends Thread { private String tag = null; public ThreadClass() { this(""); } public ThreadClass(String tag) { if (tag == null || tag.equals("")) { this.tag = ""; } else { this.tag = "[" + tag + "] : "; } } public void run() { for (int i=0; i<1000; i++) { System.out.println(tag + i); } } } /** * single thread test */ public void singleThread() { ThreadClass thread = new ThreadClass("Single Thread"); thread.start(); } /** * multi thread test */ public void multiThread() { ThreadClass thread1 = new ThreadClass("Multi Thread -1-"); ThreadClass thread2 = new ThreadClass("Multi Thread -2-"); thread1.start(); thread2.start(); } |
10.1.3. 쓰레드 우선순위
1 2 3 4 5 6 7 8 |
public void priorityThread() { ThreadClass thread1 = new ThreadClass("Multi Thread -1-"); ThreadClass thread2 = new ThreadClass("Multi Thread -2-"); thread1.setPriority(Thread.MIN_PRIORITY); thread2.setPriority(Thread.MAX_PRIORITY); thread1.start(); thread2.start(); } |
10.1.4. 쓰레드 동기화
- 동기화(synchronized) : 하나의 쓰레드만 객체에 접근할 수 있도록 객체에 락(lock)을 걸어 데이터의 일관성을 유지하는 것
- 동기화(synchronized) 비동기화(asynchronized)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
public class Account { private int totalAmount = 0; /** * 동기 메소드 * @param money */ public synchronized void synchronizedDeposit(int money) { try { totalAmount += money; Thread.sleep(1000); System.out.println(money + "원 입금, 잔액 : " + totalAmount + "원"); } catch(Exception e) { e.printStackTrace(); } } /** * 비동기 메소드 * @param money */ public void asynchronizedDeposit(int money) { try { totalAmount += money; Thread.sleep(1000); System.out.println(money + "원 입금, 잔액 : " + totalAmount + "원"); } catch(Exception e) { e.printStackTrace(); } } } public class SynchronizedThread extends Thread { private Account account; public SynchronizedThread(Account account) { this.account = account; } public void run() { Random ran = new Random(); for (int i=0; i<10; i++) { int money = (ran.nextInt(9)+1) * 1000; // 동기 메소드 호출 account.synchronizedDeposit(money); } } } public void synchronizedThread() { Account account = new Account(); SynchronizedThread thread1 = new SynchronizedThread(account); SynchronizedThread thread2 = new SynchronizedThread(account); thread1.start(); thread2.start(); } public class AsynchronizedThread extends Thread { private Account account; public AsynchronizedThread(Account account) { this.account = account; } public void run() { Random ran = new Random(); for (int i=0; i<10; i++) { int money = (ran.nextInt(9)+1) * 1000; // 비동기 메소드 호출 account.asynchronizedDeposit(money); } } } public void asynchronizedThread() { Account account = new Account(); AsynchronizedThread thread1 = new AsynchronizedThread(account); AsynchronizedThread thread2 = new AsynchronizedThread(account); thread1.start(); thread2.start(); } |