본문 바로가기
  • think normal
새로워지기/서른의 생활코딩

ex11) java_thread

by 청춘만화 2012. 3. 4.

public class MyThread7 extends Thread {
MyThread7(ThreadGroup tg, String name) {
super(tg, name); // super!! + 반드시 첫줄에 기술 //(그룹명,스레드명)
   // ->사실상 Thread(ThreadGroup group, String name)생성자 안에서 실행!
   // ->트리형식으로 묶어줌
}
public void run() {
while (true) {
try {
sleep(500);
} catch (InterruptedException e) {}
System.out.println(getName() + ": is running");
}
}
 }

class ThrGroup {
static public void main(String s[]) {
ThreadGroup MyGroup = new ThreadGroup("My Group");
MyThread7 thr_a = new MyThread7(MyGroup, "A");  //(그룹명,스레드명) -> 생성자 자동호출(위)
MyThread7 thr_b = new MyThread7(MyGroup, "B");
MyThread7 thr_c = new MyThread7(MyGroup, "C");
System.out.println("Starting the threads...");
thr_a.start();
thr_b.start();
thr_c.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
System.out.println("Stopping the thread group...");
MyGroup.stop();
}
 }

'새로워지기 > 서른의 생활코딩' 카테고리의 다른 글

java _2012.02.28  (0) 2012.03.04
ex12) java_thread  (0) 2012.03.04
ex10) java_thread  (0) 2012.03.04
ex9) java_thread  (0) 2012.03.04
ex8) java_thread  (0) 2012.03.04

댓글