Search

'데몬스레드'에 해당되는 글 2건

  1. 2009.05.14 데몬 스레드 및 스레드 확인 소스
  2. 2008.07.13 데몬스레드

데몬 스레드 및 스레드 확인 소스

컴퓨터/Java 2009. 5. 14. 16:56 Posted by 유스~*

package com.thread;

import java.util.Map;
import java.util.Set;

class T1 extends Thread {
 int i = 0;
 
 public static void dispAllstack() {
    Map<Thread,StackTraceElement[]> mpstes = Thread.getAllStackTraces();
    Set<Map.Entry<Thread,StackTraceElement[]>> es = mpstes.entrySet();
  
    for (Map.Entry<Thread,StackTraceElement[]> me : es) {
     Thread th = me.getKey();
     System.out.println("ㅁ" + th);
     StackTraceElement[] stes2 = me.getValue();
/*     for (int j=0; j<stes2.length; j++) {
      System.out.println("ㅁ " + stes2[j]);
     }*/
    }
    Thread.currentThread().getThreadGroup().list();
  }
 public void run() {
  while(i < 10) {
   try {
    sleep(1000);
   } catch(InterruptedException ie) {
    ie.printStackTrace();
   }
   System.out.println(i);
   i++;
   
   if(i == 5) {
    dispAllstack();
   }
  }
  System.out.println("활동중인 스레드수 - " +Thread.currentThread().getThreadGroup().activeCount());
  System.out.println("T1종료");
  dispAllstack();
 }

}

******************************************************************************************************

package com.thread;

class T2 extends Thread {
 
 int i = 2;
 
 public void run() {
  while(i>1) {
   try {
    Thread.sleep(1000);
   } catch(InterruptedException ie) {
    ie.printStackTrace();
   }
   System.out.println("T2");
   i++;
   
   if(i % 10 == 0) {
    Thread.currentThread().getThreadGroup().list();
    System.out.println("활동중인 스레드수 - " +Thread.currentThread().getThreadGroup().activeCount());
   }
  }
 }

}

******************************************************************************************************

package com.thread;

import java.util.*;

public class TestThread {

 public static void dispAllstack() {
    Map<Thread,StackTraceElement[]> mpstes = Thread.getAllStackTraces();
    Set<Map.Entry<Thread,StackTraceElement[]>> es = mpstes.entrySet();
  
    for (Map.Entry<Thread,StackTraceElement[]> me : es) {
     Thread th = me.getKey();
     System.out.println("ㅁ" + th);
     StackTraceElement[] stes2 = me.getValue();
   /*  for (int j=0; j<stes2.length; j++) {
      System.out.println("ㅁ " + stes2[j]);
     }*/
    }
    }
    
 public static void main(String[] args) {
  Thread.currentThread().setName("메인");
  T1 t1 = new T1();
  T2 t2 = new T2();
  t1.setName("T1");
  t2.setName("T2");
//  t2.setDaemon(true);
  t1.start();
  t2.start();
  Thread.currentThread().getThreadGroup().list();
  System.out.println("활동중인 스레드수 - " +Thread.currentThread().getThreadGroup().activeCount());
  
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  dispAllstack();
  System.out.println("main종료");
  Thread.currentThread().getThreadGroup().list();
 }
 
}

데몬스레드

컴퓨터/Java 2008. 7. 13. 16:33 Posted by 유스~*

데몬스레드에 대해 자바의정석책 설명으로는 약간 부족한면을 알게 되어서 글을 써봅니다. ㅋㅋㅋ


데몬스레드로 변경할 경우 책에서는 메인스레드가 종료시에 데몬스레드가 같이 종료하는걸로 표현되어있습니다.


 그리하여 제 설명도 메인스레드가 종료할때 데몬스레드에게 종료를 지시해서 종료시킨담에 메인스레드가 종료하는걸로 설명드렸습니다.


하지만.. 제 테스트결과 그게 아닌것을 알게 되었습니다.


스레드가 일반스레드, 데몬스레드 2종류인것을 알고 계실겁니다


테스트는 메인스레드에서 t1(데몬스레드),t2(일반스레드) 2개의 스레드를 생성해서 동작해보았습니다.


그리하여 메인스레드 종료시 t2 스레드가 계속 작동할 때 t1 스레드가 과연 종료될것인가?


그 결과는 종료가 안되었습니다. 즉 메인스레드가 t1스레드를 종료시키는게 아니라는 말입니다.


메인스레드가 종료되고 t2 스레드가 종료하자 t1스레드가 종료하는 결과를 얻게 되었습니다.


결론은 데몬스레드의 종료는 일반스레드가 모두 종료시 JVM에 의해서 종료 된다