<๊ฐ์>
runnable๋ Thread์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ณ๋ ฌ ์ํ์ ํ๊ธฐ ์ํ ๊ฒ์ด๋ค.
๋์ ์ฐจ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค. Thread๋ ํด๋์ค์ด๊ณ runnable์ ์ธํฐํ์ด์ค๋ ๊ฒ์ด๋ค. ๋จ์ผ ์์ ์์น์ ๋ฐ๋ผ Thread๋ฅผ ์์ํ ์์ ํด๋์ค๋ ๋ค๋ฅธ ๋ถ๋ชจ ํด๋์ค๋ฅผ ์์ํ์ง ๋ชปํ์ง๋ง, runnable๋ฅผ ๊ตฌํํ ํด๋์ค๋ ๋ณ๋ ฌ์ฒ๋ฆฌ ๊ธฐ๋ฅ์ ์ฐ๋ฉด์ ๋ค๋ฅธ ํด๋์ค๋ ์ธํฐํ์ด์ค๋ฅผ ์ฅ์ฐฉํ ์ ์๋ค.
1. ์ฝ๋ ๋ฆฌ๋ทฐ
import Chap_12.clean.CleanRunnable;
public class _02_Runnable {
public static void main(String[] args) {
// ์ฐ๋ ๋๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ 2๊ฐ์ง
// 1. Thread ๋ผ๋ ํด๋์ค ์์ํด์ ๋ง๋ค๊ธฐ
// 2, Runnable ์ด๋ผ๋ ์ธํฐํ์ด์ค ๊ตฌํํด์ ๋ง๋ค๊ธฐ
//1. runnable ๊ตฌํํ ํด๋์ค์ ๊ฐ์ฒด ์์ฑ
CleanRunnable cleanRunnable = new CleanRunnable();
//2. Thread ๊ฐ์ฒด ์์ฑํด์ ์์ ์ธ์๋ก ๋ฃ๊ธฐ
// ์ด๋ ๊ฒ ํ๋ ์ด์ Thread ํด๋์ค๋ ๋ฌด์กฐ๊ฑด run ๋งค์๋๋ฅผ ๊ตฌํ ํด์ฃผ์ด์ผ ํ๋ค.
// ๊ทผ๋ฐ runnable ์ธํฐํ์ด์ค ๊ตฌํํ ๊ฐ์ฒด๋ run ๋งค์๋๋ฅผ ๊ตฌํ ํ์ผ๋ฏ๋ก ์ธ์๋ก ๋ฃ์ด์ฃผ๋ฉด
// Thread๋ run์ ๊ตฌํํ ํ์๊ฐ ์๋ค.
Thread thread = new Thread(cleanRunnable);
//3. runnable ๊ฐ์ฒด๊ฐ ์๋ ๊ทธ๊ฑธ ๋ฃ์ Thread ๊ฐ์ฒด๋ฅผ ์ด์ฉ
thread.start();
cleanByBoss();
}
public static void cleanByBoss() {
System.out.println("--์ฌ์ฅ ์ฒญ์ ์์--");
for (int i = 1; i <= 10; i+=2) {
System.out.println("(์ฌ์ฅ) " + i + "๋ฒ๋ฐฉ ์ฒญ์ ์ค");
}
System.out.println("-- ์ฌ์ฅ ์ฒญ์ ๋--");
}
}
public class CleanRunnable implements Runnable{
@Override
public void run() {
System.out.println("--์ง์ ์ฒญ์ ์์(Runnable)--");
for (int i = 2; i <= 10; i+=2) {
System.out.println("(์ง์) (Runnable) " + i + "๋ฒ๋ฐฉ ์ฒญ์ ์ค");
}
System.out.println("--์ง์ ์ฒญ์ ๋(Runnable)--");
}
}
2. ์ค์ค๋ก ํด๋ณด๊ธฐ
public class RunnableGoal implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println((i+1)+"๋ฒ์งธ ๊ณจ!!!!");
}
System.out.println("๊ฒฝ๊ธฐ ๋.");
}
}
public class Runnable_myself {
public static void main(String[] args) {
RunnableGoal G = new RunnableGoal();
Thread t = new Thread(G);
t.start();
shoot();
}
public static void shoot () {
for (int i = 0; i < 10; i++) {
System.out.println((i+1)+"๋ฒ ์งธ ์!!!!");
}
System.out.println("-------------๊ฒฝ๊ธฐ ๋!------------");
}
}
** ์ฐธ๊ณ
๋ณ๋ ฌ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ ํ๋ก๊ทธ๋จ ๋๋ฆด ๋๋ง๋ค ๋ค๋ฅด๊ฒ ๋์จ๋ค.
๋งค์๋๊ฐ ์ผ์ ๋นจ๋ฆฌ ๋๋ผ ์๋ ์๊ณ Thread๊ฐ ์ผ์ ๋นจ๋ฆฌ ๋๋ผ ์๋ ์๋ค.
0