티스토리 뷰

카테고리 없음

java thread

dev ms 2016. 10. 27. 11:47
반응형

Thread 란
동작하고 있는 프로그램을 프로세스(Process)라고 한다. 보통 한 개의 프로세스는 한 가지의 일을 하지만, 이 쓰레드를 이용하면 한 프로세스 내에서 두 가지 또는 그 이상의 일을 동시에 할 수 있게 된다.

public class Test extends Thread { public void run() { System.out.println("thread run."); } public static void main(String[] args) { Test test = new Test(); test.start(); } }

Thread.Join()

쓰레드의 join 메소드는 쓰레드가 종료될 때까지 기다리게 하는 메서드이다.

public static void main(String[] args) { ArrayList<Thread> threads = new ArrayList<Thread>(); for(int i=0; i<10; i++) { Thread t = new Test(i); t.start(); threads.add(t); } for(int i=0; i<threads.size(); i++) { Thread t = threads.get(i); try { t.join(); }catch(Exception e) { } } System.out.println("main end."); }

Runnable

보통 쓰레드 객체를 만들 때 위의 예처럼 Thread를 상속하여 만들기도 하지만 보통 Runnable 인터페이스를 구현하도록 하는 방법을 많이 사용한다.
위에서 만든 예제를 Runnable 인터페이스를 사용하는 방식으로 변경 해 보자.

public class Test implements Runnable { int seq; public Test(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<Thread>(); for(int i=0; i<10; i++) { Thread t = new Thread(new Test(i)); t.start(); threads.add(t); } for(int i=0; i<threads.size(); i++) { Thread t = threads.get(i); try { t.join(); }catch(Exception e) { } } System.out.println("main end."); } }

Thread를 extends 하던 것에서 Runnable을 implements 하도록 변경되었다. (Runnable 인터페이스는 run 메소드를 구현하도록 강제한다.) 그리고 Thread 를 생성하는 부분을 다음과 같이 변경했다.

Thread t = new Thread(new Test(i));

Thread의 생성자로 Runnable 인터페이스를 구현한 객체를 넘길 수 있는데 이 방법을 사용한 것이다. 이렇게 변경된 코드는 이 전에 만들었던 예제와 완전히 동일하게 동작한다. 다만 인터페이스를 이용했으니 상속 및 기타 등등에서 좀 더 유연한 프로그램으로 발전했다고 볼 수 있겠다.


반응형