๋์ํ๊ณ ์๋ ํ๋ก๊ทธ๋จ์ ํ๋ก์ธ์ค (Process) ๋ผ๊ณ ํ๋ค.
ํ ๊ฐ์ ํ๋ก์ธ์ค์์๋ ํ ๊ฐ์ง์ ์ผ์ ํ์ง๋ง
์ค๋ ๋(Thread) ๋ฅผ ์ด์ฉํ๋ฉด ํ๋์ ํ๋ก์ธ์ค ์์ ๋ ๊ฐ ์ด์์ ์ผ์ ๋์์ ์งํํ ์ ์๋ค.
public class Sample extends Thread {
public void run() { // Thread ๋ฅผ ์์ํ๋ฉด run ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํ๋ค.
System.out.println("thread run.");
}
public static void main(String[] args) {
Sample sample = new Sample();
sample.start(); // start()๋ก ์ฐ๋ ๋๋ฅผ ์คํํ๋ค.
}
}
์ด๋ค ํด๋์ค์์ ์ค๋ ๋๋ฅผ ์์ ๋ฐ์ผ๋ฉด run () ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํ๋ค.
main ๋ฉ์๋์์ start ํด์ค๋ค.
๋ฉ์ธ ๋ฉ์๋์์ start ๋ฅผ ํด์ฃผ๋ ์๊ฐ, ์ค๋ ๋์ ์๋ run ์ด ์๋๋๋ ์๋ฆฌ์ด๋ค.
๋ฉ์ธ ๋ฉ์๋๊ฐ ์ข
๋ฃ๋์ด๋ ์ค๋ ๋๋ ๋์ํ๋ค.
๋์์ ์์ํด๋ ์์๋ ๋ณด์ฅ๋์ง ์๊ณ , ๋๊ฐ์ด ๋์ํ๋ ์ค๋ ๋๋ฅผ ๋์์ 10๊ฐ๋ฅผ ์คํ์์ผฐ์ ๋
์ด๋ค ์ค๋ ๋๊ฐ ๋จผ์ ๋๋ ์ง๋ ๋ชจ๋ฆ.
์ค๋ ๋๋ฅผ ์ฐจ๋ก๋๋ก ์คํ์ํค๊ณ ์ถ์ผ๋ฉด ?
join ์ ๊ฑธ๋ฉด ๋๋ค.
import java.util.ArrayList;
public class Sample extends Thread {
int seq;
public Sample(int seq) {
this.seq = seq;
}
public void run() {
System.out.println(this.seq+" thread start.");
try {
Thread.sleep(1000);
}catch(Exception e) {
}
System.out.println(this.seq+" thread end.");
}
public static void main(String[] args) {
ArrayList<Thread> threads = new ArrayList<>();
for(int i=0; i<10; i++) {
Thread t = new Sample(i);
t.start();
threads.add(t);
}
for(int i=0; i<threads.size(); i++) {
Thread t = threads.get(i);
try {
t.join(); // t ์ฐ๋ ๋๊ฐ ์ข
๋ฃํ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
}catch(Exception e) {
}
}
System.out.println("main end.");
}
}
์ค๋ ๋ ๊ฐ์ฒด t ํ๋์ฉ ์คํ๋๋ค.
๊ฐ ์ค๋ ๋ ์์ ์๋ run () ์คํ์ด ์๋ฃ ๋๋ฉด ๊ทธ ๋ค์ ์ค๋ ๋๊ฐ ์คํ ๋จ.
tcp ํต์ ์ผ๋ก ์๋ฐ ์ฝ์์ฐฝ์ ์ฑํ
๋ฐฉ ๊ตฌํํ๊ณ ์๋๋ฐ
์ค๋ ๋ ๊ฐ๋
์ ํท๊ฐ๋ คํ๋ ๊ฒ ๊ฐ์์ ๊ฐ์ฅ ๊ธฐ์ด์ ์ธ ์ค๋ ๋ ๊ฐ๋
๋ถํฐ ๋ค์ ๋ดค๋ค. ๊ทผ๋ฐ ์ฑํ
๋ฐฉ ๊ตฌํํ ๋ join ์ธ ์ผ์ ์์๋ ๊ฑฐ ๊ฐ์๋ฐ..