2016-05-25 5 views
2

私は、スレッドがデーモンでも非デーモンでもよいことを知っています。 isDaemon()メソッドを使ってスレッドがデーモンかどうかを調べることができます。 isDaemon()メソッドはスレッドグループに対しても機能します。JAVAのデーモンスレッドグループとは何ですか?

class MyThread extends Thread 
{ 
MyThread(ThreadGroup g, String name) 
{ 
    super(g,name); 
} 
public void run() 
{ 
    long i = 0; 
    for(long l=0; l<999999999; l++) 
    { 
    i=i+3; 
    } 
} 
} 

class Check 
{ 
public static void main(String[] args) 
{ 
    ThreadGroup sys = Thread.currentThread().getThreadGroup().getParent(); 
    ThreadGroup parent = new ThreadGroup("parent"); 
    MyThread t1 = new MyThread(parent, "t1"); 
    ThreadGroup child = new ThreadGroup(parent,"child"); 
    Thread t2 = new Thread(child, "t2"); 
    t1.start(); 
    t2.start(); 
    ThreadGroup[] t = new ThreadGroup[sys.activeGroupCount()]; 
    sys.enumerate(t); 
    for(ThreadGroup ti: t) 
    { 
    System.out.println(ti.getName()+" "+ti.isDaemon()); 
    } 
    System.out.println(sys.getName()+" "+sys.isDaemon()); 
} 

出力:ここ

main false 
parent false 
child false 
system false 

システムは、非デーモンスレッドグループです。スレッドグループはどのようにデーモンになりますか?デーモンスレッドグループのプロパティは何ですか?システムスレッドグループはどのように非デーモンですか?

+0

は、私はあなたの質問が間違って得たかもしれないが、あなたはこの 'sys.setDaemon(真)のような任意のスレッドデーモンを作ることができます;' – Helios

+0

私は「スレッド」の話ではないのです。私は "スレッドグループ"のデーモンと非デーモンの性質について話しています。 –

+0

スレッドグループについても同様です – Helios

答えて

3

スレッドと同じ方法:java.lang.ThreadGroup#setDaemon。スレッドグループを作成するときは、それをデーモンとしてマークすることができます。

Javadocを1として:その最後のスレッド が停止しているか、その最後のスレッドグループが破棄されると、デーモンスレッドグループは自動的に破棄され

+0

ありがとうございます。しかし、デーモンスレッドグループのプロパティを知りたいです。 –

+1

javadocによると、デーモンスレッドグループは、最後のスレッドが停止したとき、または最後のスレッドグループ破壊される。 – Nikem

1

はい、スレッドグループをデーモンスレッドとして設定できます。

/** 
* Changes the daemon status of this thread group. 
* <p> 
* First, the <code>checkAccess</code> method of this thread group is 
* called with no arguments; this may result in a security exception. 
* <p> 
* A daemon thread group is automatically destroyed when its last 
* thread is stopped or its last thread group is destroyed. 
* 
* @param  daemon if <code>true</code>, marks this thread group as 
*      a daemon thread group; otherwise, marks this 
*      thread group as normal. 
* @exception SecurityException if the current thread cannot modify 
*    this thread group. 
* @see  java.lang.SecurityException 
* @see  java.lang.ThreadGroup#checkAccess() 
* @since  JDK1.0 
*/ 
関連する問題