โ Thread๋?
โ Thread ์์ฑ ๋ฐฉ๋ฒ
1) Thread ํด๋์ค๋ฅผ ์์ํ๊ฑฐ๋2) Runnable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ๊ฒclass MyThread extends Thread {
public void run() {
// ์คํํ ์์
์์ฑ
}
}
โ run() ๋ฉ์๋
๋จ์ํ ๋ฉ์๋ ์คํ์ด ๋๊ณ ,์ง์ง ์ค๋ ๋๋ก ์คํ๋ฉ๋๋ค.@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("MyThread: " + i);
}
}
โ sleep()์ผ๋ก ์ผ์ ์ ์ง
Thread.sleep(๋ฐ๋ฆฌ์ด)๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ ๋๋ฅผ ์ ๊น ๋ฉ์ถ ์ ์์ต๋๋ค.500์ 0.5์ด ๋์ ์ผ์ ์ ์งํฉ๋๋ค.์์ธ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ฏ๋ก try-catch๋ก ๊ฐ์ธ์ผ ํฉ๋๋ค.try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
โ Thread ์คํ
start()๋ฅผ ํธ์ถํ๋ฉด ์๋ก์ด ์ค๋ ๋์์ run()์ด ์คํ๋ฉ๋๋ค.run()์ ์ง์ ํธ์ถํ๋ฉด ๋ฉํฐ์ค๋ ๋ฉ์ด ๋์ง ์๊ณ ์ผ๋ฐ ๋ฉ์๋์ฒ๋ผ ์์ฐจ ์คํ๋ฉ๋๋ค.class MyThread extends Thread {
@Override
public void run() {
for(int i = 1; i <= 5; i++) {
System.out.println("My Thread: " + i);
try {
Thread.sleep(5000);
} catch(InterruptedException e) {
System.out.println("MyThread was interrupted.");
}
}
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
๐ค ๋ง์ฝ run()๊ณผ start()๋ฅผ ๋์์ ํธ์ถํ๋ค๋ฉด?
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
thread.run();
}
}
โ๏ธ start() vs run() ์ฐจ์ด์
| ๊ตฌ๋ถ | start() | run() |
|---|---|---|
| ์คํ ๋ฐฉ์ | ์๋ก์ด ์ค๋ ๋๋ฅผ ์์ฑํ๊ณ ๋ด๋ถ์ ์ผ๋ก run() ์คํ | ๋จ์ํ ๋ฉ์๋ ํธ์ถ (์ค๋ ๋ ์๋) |
| ๋ณ๋ ฌ ์ฒ๋ฆฌ | O (์ค์ ๋ฉํฐ์ค๋ ๋ ๋์) | X (ํ์ฌ ์ค๋ ๋์์ ์คํ๋จ) |
| ์ฌ์ฉ ๋ชฉ์ | ์ค๋ ๋๋ฅผ ์์ํ ๋ ์ฌ์ฉ | ์คํํ ์์ ๋ด์ฉ์ ์ ์ํ ๋ ์ฌ์ฉ |
| ์คํ ๊ฒฐ๊ณผ | ์๋ก์ด ์คํ ํ๋ฆ์ด ์๊ฒจ run()์ด ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์คํ๋จ | ๊ทธ๋ฅ ์ผ๋ฐ ๋ฉ์๋์ฒ๋ผ ์คํ๋จ |
๐ Thread ์ฃผ์ ๋ฉ์๋ ์์ฝ
| ๋ฉ์๋ | ์ค๋ช |
|---|---|
| run() | ์ค๋ ๋์์ ์ค์ ๋ก ์คํํ ์์ ์ ์ ์ํ๋ ๋ฉ์๋ |
| start() | ์๋ก์ด ์ค๋ ๋๋ฅผ ์์ฑํ๊ณ ๋ด๋ถ์ ์ผ๋ก run() ์คํ |
| sleep(ms) | ์ง์ ๋ ์๊ฐ(ms) ๋์ ์ค๋ ๋๋ฅผ ์ผ์ ์ ์ง์ํด |
| interrupt() | ๋ค๋ฅธ ์ค๋ ๋์ ์ค๋จ ์์ฒญ์ ๋ณด๋ด๋ฉฐ, sleep() ์ค์ด๋ฉด InterruptedException ๋ฐ์ |
โ Runnable์ด๋?
๋์์ ๋ค๋ฅธ ํด๋์ค๋ฅผ ์์ํ ์ ์์ด์ ๋ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.โ
Runnable ํด๋์ค ์์ฑ
Runnable์ ๊ตฌํํ ํด๋์ค์์๋ ๋ฐ๋์ run() ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํ๋ฉฐ,
์ด ๋ฉ์๋ ์์ ์ค๋ ๋๊ฐ ์คํํ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable ์คํ ์ค");
}
}
โ
Runnable ์คํ ๋ฐฉ๋ฒ
Runnable์ ์คํํ ์ฝ๋๋ง ์ ์ํ๋ฉฐ, ์ง์ ์คํํ์ง ์์ต๋๋ค.
Thread ๊ฐ์ฒด์ ์ ๋ฌํ ํ start()๋ฅผ ํธ์ถํ๋ฉด ๋ฉํฐ์ค๋ ๋๋ก ์คํ๋ฉ๋๋ค.
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}
โ Thread.sleep()๊ณผ ์์ธ ์ฒ๋ฆฌ
Thread.sleep()์ ์ฌ์ฉํฉ๋๋ค.InterruptedException์ ๋์ง ์ ์์ผ๋ฏ๋ก try-catch๋ก ๊ฐ์ธ์ผ ํฉ๋๋ค.try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("์ค๋ ๋๊ฐ ์ค๋จ๋์์ต๋๋ค.");
}
โ Thread ์ด๋ฆ
thread.setName(), thread.getName() ๋งค์๋๋ก ์ด๋ฆ์ ์ง์ , ์ด๋ฆ์ ๋ฐ์์ฌ ์ ์๋ค.class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable executed");
for(int i = 1; i <= 5; i++) {
try {
Thread.sleep(5000);
System.out.println(i);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
class MyThread extends Thread {
@Override
public void run() {
System.out.println(getName() + " Thread executed");
}
}
public class Main {
public static void main(String[] args) {
Thread firstThread = new Thread(new MyRunnable(), "First Thread");
firstThread.start();
Thread secondThread = new MyThread();
secondThread.setName("second");
secondThread.start();
}
}
โ ๋๋ค์์ผ๋ก runnable ์ง์ ๊ตฌํํ๊ธฐ
public class Main {
public static void main(String[] args) {
new Thread(()-> {
System.out.println("Runnable ์คํ ์ค");
for(int i = 1; i <= 5; i++) {
try {
Thread.sleep(5000);
System.out.println(i);
} catch(InterruptedException e) {
System.out.println(e.getMessage());
}
}
}).start();
}
}
โ๏ธ ์์ : Hello ์ถ๋ ฅ
Runnable์ ์ฌ์ฉํด 0.5์ด๋ง๋ค 'Hello'๋ฅผ ์ถ๋ ฅํ๋ ๊ฐ๋จํ ์์ ์
๋๋ค.
run() ๋ฉ์๋ ์์์ ๋ฐ๋ณต๋ฌธ๊ณผ sleep()์ ํจ๊ป ์ฌ์ฉํฉ๋๋ค.
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Hello from Thread: " + i);
try { Thread.sleep(500); } catch (InterruptedException e) {
System.out.println("Thread was interrupted.");
}
}
}
}
โ๏ธ ์์ : ๋ฐฐ์ด ํฉ๊ณ ๊ณ์ฐ
๊ฐ๊ธฐ ๋ค๋ฅธ ๋ฐฐ์ด์ ๊ฐ๊ฐ์ ์ค๋ ๋์์ ๋์์ ํฉ์ฐํ๋ ์์ ์
๋๋ค.
Runnable์ ๊ตฌํํด run() ์์์ ๋ฐฐ์ด์ ํฉ์ ๊ณ์ฐํ๊ณ ์ถ๋ ฅํฉ๋๋ค.
class SumRunnable implements Runnable {
private final int[] numbers;
SumRunnable(int[] numbers) {
this.numbers = numbers;
}
@Override
public void run() {
int sum = 0;
System.out.println("SumRunnable executed");
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("SumRunnable interrupted");
}
}
System.out.println(Thread.currentThread().getName() + ": SumRunnable returned " + sum);
}
}
public class Main {
public static void main(String[] args) {
int[][] dataSets = {
{1, 2, 3, 4, 5},
{10, 20, 30, 40, 50},
{7, 14, 21, 28},
{100, 200, 300, 400}
};
for (int[] dataSet : dataSets) {
Thread sumThread = new Thread(new SumRunnable(dataSet));
sumThread.start();
}
}
}
โ ๋๊ธฐํ(synchronization)๋?
โ ์ค๋ ๋ ์์ฑ ๋ฐ ์คํ
class Counter {
private int count = 0;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Runnable task = () -> {
for(int i = 0; i < 10000; i++) {
counter.increment();
}
};
Thread[] threads = new Thread[5];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(task);
threads[i].start();
}
for(Thread t: threads) {
t.join(); // ๊ฐ ์ค๋ ๋๋ฅผ ๊ธฐ๋ค๋ ค์ค๋ค.
}
System.out.println("Final count: " + counter.getCount());
}
}
โถ๏ธ
Final count: 47989
โ ์คํ ๊ฒฐ๊ณผ ์์ธก
โน๏ธ Race Condition
๊ฒฝ์ ์กฐ๊ฑด
โ synchronized๋ก ํด๊ฒฐ
ํ ๋ฒ์ ํ๋์ ์ค๋ ๋๋ง ์ด ๋ฉ์๋๋ฅผ ์คํํ ์ ์์ด ๊ฐ์ด ์ํค๋ ๋ฌธ์ ๋ฅผ ๋ง์ ์ ์์ต๋๋ค.public synchronized void increment() {
count++;
}
โ ๋๊ธฐํ ์ ์ฉ ํ ๊ฒฐ๊ณผ
Final count: 50000
โ๏ธ synchronized ์ฌ์ฉ ์ /ํ ๋น๊ต
| ์กฐ๊ฑด | ๋๊ธฐํ ์ | ๋๊ธฐํ ํ |
|---|---|---|
| count ์ต์ข ๊ฐ | ์คํ๋ง๋ค ๋ฌ๋ผ์ง ์ ์์ (์: 4266) | ํญ์ 5000์ผ๋ก ์ ํํจ |
| ์ถฉ๋ ๊ฐ๋ฅ์ฑ | ๋์ (์ฌ๋ฌ ์ค๋ ๋๊ฐ ๋์์ ์ ๊ทผ) | ์์ (์์ฐจ์ ์ผ๋ก ์ ๊ทผ) |
| ์ฝ๋ ์์ ์ฑ | ๋ฎ์ (์์์น ๋ชปํ ๊ฒฐ๊ณผ ๋ฐ์) | ๋์ (์ผ๊ด๋ ๊ฒฐ๊ณผ ๋ณด์ฅ) |
โ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋?