10

別のスレッド内でスレッドを作成して開始すると、それは子スレッドになりますか? 子スレッドの実行中に親スレッドが終了するのを防ぎますか? 例えば:

new Thread(new Runnable() {
    @Override
    public void run() {
        //Do Sth
        new Thread(new Runnable() {
            @Override
            public void run() {
                //Do Sth
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //Do Sth
                    }
                }).start();
                //Do Sth
            }
        }).start();
        //Do Sth
    }
    //Do Sth            
}).start();
4

4 に答える 4

15

あなたのコードでは、オブジェクト間に親子関係があります。ただし、スレッド間に親子関係という概念はありません。2 つのスレッドが実行されると、それらは基本的にピアになります。

スレッド A がスレッド B を開始するとします。それらの間に明示的な同期がない限り、いずれかのスレッドは、他のスレッドに影響を与えることなく、いつでも終了できます。

どちらのスレッドも、スレッドが存続している限り、プロセスを存続させることができることに注意してください。Java のデーモン スレッドとはを参照してください。

于 2013-11-14T20:55:45.590 に答える
6

別のスレッド内でスレッドを作成して開始すると、それは子スレッドになりますか?

Java には、「子」スレッドの実際の概念はありません。スレッドを開始すると、スレッドは「親」からデーモンと優先度を継承しますが、それは親子関係の終わりです。

// in Thread.init()
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();

スレッドが開始されると、スレッドはその「親」に沿って実行され、2 つの間にリンケージはありません。

子スレッドの実行中に親スレッドが終了するのを防ぎますか?

いいえ、違います。しかし、スレッドがアプリケーションの終了を防ぐことができるかどうか本当に疑問に思っていると思います。子スレッドが非デーモンの場合、メイン スレッド (これも非デーモン) が終了しても、アプリケーションは子スレッドが終了するまで待機します。定義上、JVM はすべての非デーモン スレッドが終了するまで待機します。最後の非デーモン スレッドが終了すると、JVM を終了できます。

参照: Java のデーモン スレッドとは

于 2013-11-14T23:54:33.200 に答える
0

2 つのスレッドに親子関係はありません。の完了後ParentThreadParentThreadを待機しません ChildThread

public class MainThread {
    public static void main(String[] args) {
         ParentThread pt=new ParentThread();
         pt.start();
         for(int i=1;i<=20;i++){
             System.out.println("ParentThread alive: "+pt.isAlive());
             try{
                 Thread.currentThread().sleep(500);
             }catch(Exception ex){
                 ex.printStackTrace();
             }
         }          
     }
 }

class ParentThread extends Thread{
    public void run(){
        ChildThread ct=new ChildThread();
        ct.start();
        for(int i=1;i<=10;i++){
            try{
                System.out.println("ChildThread alive: "+ ct.isAlive() + ", i = "+i);
                sleep(500);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }   
    } 
}
class ChildThread extends Thread{
    public void run(){
        for(int i=1;i<=20;i++){
            try{
                System.out.println("ChildThread i = "+i);
                sleep(500);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
}

出力(好き):

ParentThread alive: true
ChildThread alive: true, i = 1
ChildThread i = 1
ParentThread alive: true
ChildThread i = 2
ChildThread alive: true, i = 2
ParentThread alive: true
ChildThread i = 3
ChildThread alive: true, i = 3
ParentThread alive: true
ChildThread i = 4
ChildThread alive: true, i = 4
ParentThread alive: true
ChildThread i = 5
ChildThread alive: true, i = 5
ParentThread alive: true
ChildThread i = 6
ChildThread alive: true, i = 6
ParentThread alive: true
ChildThread alive: true, i = 7
ChildThread i = 7
ParentThread alive: true
ChildThread i = 8
ChildThread alive: true, i = 8
ParentThread alive: true
ChildThread alive: true, i = 9
ChildThread i = 9
ParentThread alive: true
ChildThread alive: true, i = 10
ChildThread i = 10
ParentThread alive: true
ChildThread i = 11
ParentThread alive: false
ChildThread i = 12
ChildThread i = 13
ParentThread alive: false
ParentThread alive: false
ChildThread i = 14
ParentThread alive: false
ChildThread i = 15
ParentThread alive: false
ChildThread i = 16
ParentThread alive: false
ChildThread i = 17
ChildThread i = 18
ParentThread alive: false
ParentThread alive: false
ChildThread i = 19
ParentThread alive: false
ChildThread i = 20
于 2018-01-23T08:27:53.493 に答える