1. μ€λ λ(Thread)λ?
- μ΄λ€ νλ‘μΈμ€ λ΄μμ μ€νλλ νλ¦(μμ )μ λ¨μλ€.
- νλ‘μΈμ€ λ΄ λͺ λ Ήμ΄ λΈλ‘μΌλ‘ μμ, μ’ λ£μ μ κ°μ§λ€.
- JVMμ μν΄ νλμ νλ‘μΈμ€κ° λ°μνκ³ main λ΄λΆμ μ€νλ¬Έμ΄ νλμ μ€λ λλ€.
- main() μ΄μΈμ λ€λ₯Έ μ€λ λλ₯Ό λ§λ€λ €λ©΄ Thread ν΄λμ€λ₯Ό μμνκ±°λ Runnalble μΈν°νμ΄μ€λ₯Ό ꡬννλ€.
- λ€μ€ μ€λ λ μμ μ κ° μ€λ λλΌλ¦¬ μ 보λ₯Ό μ£Όκ³ λ°μ μ μμ΄ μ²λ¦¬ κ³Όμ μ μ€λ₯λ₯Ό μ€μΌ μ μλ€.
- νλ‘μΈμ€λΌλ¦¬λ μ 보λ₯Ό μ£Όκ³ λ°μ μ μλ€.
2. λ©ν°μ€λ λ©μ΄λ?
μ¬λ¬ μ€λ λλ₯Ό λμμ μ€νμν€λ μμ©νλ‘κ·Έλ¨μ μμ±νλ κΈ°λ²μ΄λ€.
μ₯μ
-
λ©λͺ¨λ¦¬ 곡μ λ‘ μΈν μμ€ν μμ μλͺ¨κ° μ€μ΄λ λ€.
-
λμμ λ κ°μ§ νλμ νλ κ²μ΄ κ°λ₯ν΄μ§λ€.
λ¨μ
-
μλ‘ μμμ μλͺ¨νλ€κ° μΆ©λμ΄ μΌμ΄λ κ°λ₯μ±μ΄ μ‘΄μ¬νλ€.
-
μ½λκ° λν΄ν΄μ Έ λ²κ·Έμμ±νλ₯ μ΄ λμμ§λ€.
3. μ€λ λ μμ±μ£ΌκΈ°
New : μΈμ€ν΄μ€λ λ§λ€μ΄μ‘μΌλ μμ§ μ€νμ΄ λμ§ μμ μν
Runnable (μ€λΉμν) : start() λ©μλ νΈμΆμ μν΄ μ€λ λλ Runnable μνλ‘ μ΄λνλ€.
Running (μ€νμν) : μ€μΌμ€λ¬λ Runnable pool 체ν¬νκ³ μ€λ λλ₯Ό Running μνλ‘ μ΄λμν¨λ€.
Blocked (μ§μ°μν) : sleep(), wait() λ©μλ, I/O μ μΆλ ₯ μμ², λκΈ°μνλ‘ μ΄λνλ€.
Dead (μ’ λ£μν) : μ€νμ΄ μλ£λλ©΄ μ€λ λλ μ’ λ£λκ³ μ΄ μνμμ λ€μ μμλλκ²μ λΆκ°λ₯νλ€.
4. synchronized
<λ©μλλ₯Ό synchronizedλ‘ μ μΈ>
synchronizedκ° λ©μλ μ μΈλΆμ μμΌλ©΄ ν΄λΉ κ°μ²΄μ λ©μλμ 2κ°μ μ°λ λκ° μ κ·Όνλ , 100κ°μ μ°λ λκ° μ κ·Όνλ κ°μ ν μκ°μλ νλμ μ°λ λλ§ μ΄ λ©μλλ₯Ό μννκ² λλ€.
- CommonCalculate ν΄λμ€μ κ°μ²΄λ₯Ό calcλΌλ μ΄λ¦μΌλ‘ μμ±
- ModifyAmountThreadλΌλ ν΄λμ€ κ°μ²΄λ₯Ό μμ±ν λ calcλ₯Ό 맀κ°λ³μλ‘ λκΈ°κ³ plus() λ©μλλ§ μννλλ‘ trueλ₯Ό λλ²μ§Έ 맀κ°λ³μλ‘ λκΉ
- κ°κ°μ μ€λ λ μ€ν
- try-catch λΈλ‘ μμμ join()μ΄λΌλ λ©μλλ₯Ό κ°κ°μ μ€λ λμ λν΄ νΈμΆνλ€. [join : μ€λ λκ° μ’ λ£λ λκΉμ§ κΈ°λ€λ¦¬λ λ©μλ]
- join()μ΄ λλλ©΄ calc κ°μ²΄μ getAmount() λ©μλλ₯Ό νΈμΆνλ€. getAmount() λ©μλμ νΈμΆ κ²°κ³Όλ join() λ©μλ μν μ΄νμ΄λ―λ‘ λͺ¨λ μ€λ λκ° μ’ λ£λ μ΄νμ κ²°κ³Όλ€.
<μλ μ½λμ κ²°κ³Ό μμ>
Main ν΄λμ€μ runCommonCalculate() λ©μλκ° μνλ ν λκ°μ μ€λ λμμ νλμ κ°μ²΄μ μλ amountλΌλ int νμ μ κ°μ 1μ λ§λ² λν΄ 20000μ΄ μΆλ ₯λμ΄μΌνλ€.
public class Main {
public static void main(String[] args) {
Main runSync=new Main();
for (int i = 0; i < 5; i++)
runSync.runCommonCalculate();
}
public void runCommonCalculate() {
CommonCalculate calc=new CommonCalculate();
ModifyAmountThread thread1 = new ModifyAmountThread(calc,true);
ModifyAmountThread thread2 = new ModifyAmountThread(calc,true);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
System.out.println("Final value is "+calc.getAmount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class CommonCalculate {
private int amount;
public CommonCalculate() {
amount = 0;
}
public void plus(int value) {
amount += value;
}
public void minus(int value) {
amount -= value;
}
public int getAmount() {
return amount;
}
}
public class ModifyAmountThread extends Thread {
private CommonCalculate calc;
private boolean addFlag;
public ModifyAmountThread(CommonCalculate calc, boolean addFlag){
this.calc=calc;
this.addFlag=addFlag;
}
public void run() {
for (int loop = 0; loop < 10000; loop++) {
if(addFlag) {
calc.plus(1);
} else {
calc.minus(1);
}
}
}
}
νμ§λ§ κ²°κ³Όλ λ€μκ³Ό κ°μ΄ λμ¨λ€.Final value is 20000
Final value is 17824
Final value is 14589
Final value is 19747
Final value is 20000
20000μ΄ λμ¬λλ, λμ€μ§ μμλλ μλ€.
<μ μ΄λ° νμμ΄ μΌμ΄λλκ°?>
plus()λ©μλλ λ€λ₯Έ μ°λ λμμ μμ νκ³ μλλΌλ, μλ‘μ΄ μ€λ λμμ μ¨ μμ λ κ°μ΄ μ²λ¦¬νκΈ° λλ¬Έμ λ°μ΄ν°κ° κΌ¬μΌ μ μλ€.
μν창ꡬλ₯Ό μλ‘λ€λ©΄ νλͺ μ μ§μμ΄ νλμ 창ꡬμμ ν κ³ κ°μ μμ²λ§ μ²λ¦¬νλλ°, λ§μ½ νλ²μ μ¬λ¬ κ³ κ°μ μμ²μ μ²λ¦¬νλ©΄ ν΄λΉ 창ꡬλ κ³ κ°μ μμ²μ΄ λ€μ£½λ°μ£½ λμ΄ ν건λ μ λλ‘ μ²λ¦¬ λͺ»νκ² λ μ μλ€.
μλμ²λΌ synchronizedλ₯Ό plus() λ©μλμ μΆκ°ν΄μ£Όλ©΄ μμ μ€λ λμμ μννκ³ μλ λ©μλκ° λλ λ κΉμ§ λ¦κ²μ¨ μ€λ λλ κΈ°λ€λ¦¬κ² λλ€.
public synchronized void plus(int value) {
amount += value;
}
public synchronized void minus(int value) {
amount -= value;
}
λ©μλμ μΆκ°νλ λ°©λ²μ΄ μλ λ€μκ³Ό κ°μ λ°©λ²λ μ‘΄μ¬νλ€.
μ΄λ κ² synchronizedλ₯Ό μΆκ°ν΄μ£Όλ©΄ λκΈ°μκ°μ ν¨μ¨μ μΌλ‘ κ΄λ¦¬ν μ μλ€.
public void plus(int value) {
synchronized (this) {
amount += value;
}
}
public void minus(int value) {
synchronized (this) {
amount -= value;
}
}
4. μμ
1. Single Thread (extends Thread)
import java.util.*;
public class SingleTreadEx extends Thread {
private int[] temp;
public SingleTreadEx(String threadname) {
super(threadname);
temp = new int[10];
for (int start=0; start<temp.length; start++) {
temp[start]=start;
}
}
public void run() {
for (int start:temp) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Thread Name :"+currentThread().getName());
System.out.println("temp value :"+start);
}
}
public static void main(String[] args) {
SingleTreadEx st = new SingleTreadEx("1λ² μ€λ λ");
st.start();
}
}
2. Single Thread (implements Runnable)
public class SingleTreadEx implements Runnable {
private int[] temp;
public SingleTreadEx(){
temp = new int[10];
for(int start=0;start<temp.length;start++){
temp[start]=start;
}
}
@Override
public void run() {
for(int start:temp){
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Thread Name :"+Thread.currentThread().getName());
System.out.println("temp value :"+start);
}
}
public static void main(String[] args) {
SingleTreadEx ct = new SingleTreadEx();
Thread t = new Thread(ct,"1λ² μ€λ λ");
t.start();
}
}
3. Multi Thread
class ATM implements Runnable {
private long depositeMoney = 10000;
public void run() {
synchronized (this) {
for (int i = 0; i < 10; i++) {
notify();
try {
wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (getDepositeMoney() <= 0)
break;
withDraw(1000);
}
}
}
public void withDraw(long howMuch) {
if (getDepositeMoney() > 0) {
depositeMoney -= howMuch;
System.out.print(Thread.currentThread().getName() + " , ");
System.out.printf("μμ‘ : %,d μ %n", getDepositeMoney());
} else {
System.out.print(Thread.currentThread().getName() + " , ");
System.out.println("μμ‘μ΄ λΆμ‘±ν©λλ€.");
}
}
public long getDepositeMoney() {
return depositeMoney;
}
}
public class SynchronizedEx {
public static void main(String[] args) {
ATM atm = new ATM();
Thread mother = new Thread(atm, "Junho");
Thread son = new Thread(atm, "Wonbeen");
mother.start();
son.start();
}
}
*Reference
μ΄μλ―Ό / μλ°μ μ vol.1 / λ‘λλΆ / 2013λ 2μ
'π¬ Language > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
GSON - com.google.gson.stream.MalformedJsonException (0) | 2022.08.02 |
---|---|
Exception Handling (2) | 2020.05.06 |
Overloadingκ³Ό Overriding (0) | 2020.01.30 |
Calculator μ€μ΅ μμ± (0) | 2020.01.18 |
StringTokenizer (0) | 2020.01.18 |