2011. 10. 13. 14:48

자바에서 `쓰레드(Thread)`를 발생시키는 두 가지 형태

1. 상속을 통한 방법
package net.br;
publicclass Thread1 extends Thread {
publicvoid run() {  
int num = 0 ;    
for(num = 1 ; num <= 26 ; num++) {
   System.out.print(num) ;   
  }
 }
}

2. 인터페이스를 이용한 방법

package net.br;
publicclass Thread2 implements Runnable {
publicvoid run() {  
char ch = ' ' ;    
for(ch = 'A' ; ch <= 'Z' ; ch++) {
   System.out.print(ch) ;
  }
 }
}

3. 쓰레드 발생은 `thread.start()` 메써드로 한다.

package net.br;
publicclass MakeThread {
publicstaticvoid main(String[] args) {
  Thread1 thread1 = new Thread1() ;
 Thread2 thread2 =
new Thread2() ;
  Thread th1 = new Thread(thread1) ;
  Thread th2 = new Thread(thread2) ;  
  th1.start() ;
  th2.start() ;  
 }
}

결과 값의 다양한 형태

1 : 12345678910111213141516171819ABCDEFGHIJKLMNOPQRSTUVWXYZ20212223242526
2 : 1234567891011121314151617181920212223242526ABCDEFGHIJKLMNOPQRSTUVWXYZ
3 : AB1234567891011121314151617181920212223242526CDEFGHIJKLMNOPQRSTUVWXYZ
4 : 123ABCDEFGHIJKLMNOPQRSTUVWXYZ4567891011121314151617181920212223242526
5 : ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567891011121314151617181920212223242526