如何让主端持续? [英] How to make the main end last?

查看:48
本文介绍了如何让主端持续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让主进程最后结束?

例如我写了一些代码,它创建了一个线程:Test,他创建了另外三个线程 - Test2,但是 main 在 Test 开始了.

For example I write some code, which created one Thread: Test, who created another three threads - Test2, but main finished before Test started.

public class Test implements Runnable{
    String url;
    Thread mThread;

    public Test(String url) {
        this.url = url;        
    }

    public void start(){
       this.mThread = new Thread(this);
       this.mThread.start();
    }

     @Override
    public void run() {
        System.out.println("TEST STARTED!");
        Test2 wclw[] = new Test2[3];

            for (int i = 0; i < wclw.length; i++) {
                wclw[i] = new Test2(url, i + 1);
                wclw[i].start();
            }
    }

    public static void main(String[] args) throws IOException {
        System.out.println("MAin STARTED!");
        (new Test("qwerty")).start();

        System.out.println("MAIN FINISHED");
    }
}

class Test2 implements Runnable{
    String url;
    int threadNum;
    Thread mThread;

    public Test2(String url, int threadNum) {
        this.url = url;
    }

    public void start(){
        this.mThread = new Thread(this);
        this.mThread.start();
    }   

    @Override
    public void run() {
         System.out.println("TEST2 STARTED!");   
         for (int i = 0; i < 2; i++) {
            System.out.println(url);
        }
    }
}

输出:

MAin STARTED! 
MAIN FINISHED 
TEST STARTED!
qwerty
TEST2 STARTED!
TEST2 STARTED!
qwerty
qwerty 
TEST2 STARTED!
qwerty 
qwerty
qwerty

推荐答案

如何让主进程最后结束?

How to make the main processs ended last?

我假设您指的是主线程.您需要使用 thread.join().您的主线程应该与它产生的线程连接,而子线程也需要与它产生的线程连接.thread.join() 在线程继续之前等待线程完成.

I assume you mean the main thread. You need to use thread.join(). Your main should join with the thread that it spawns and the sub-thread needs to join with the threads that it spawns as well. thread.join() waits for the thread to finish before it continues.

Test test = new Test("qwerty");
// start the test thread running in the background
test.start();
// do other stuff on the main thread
...
// wait for the test thread to finish
test.mThread.join();

在测试线程中你应该这样做:

Inside the test thread you should do:

// start other threads
for (int i = 0; i < wclw.length; i++) {
    wclw[i] = new Test2(url, i + 1);
    wclw[i].start();
}
// do other stuff in the test thread if necessary
...
// come back and wait for them all to finish
for (int i = 0; i < wclw.length; i++) {
    wclw[i].mthread.join();
}

所以子线程将等待它产生的每个线程完成,然后它就会完成.主线程会等待子线程完成,然后最后完成.

So the sub-thread will wait for each of the threads that it spawned to finish and then it will finish. The main thread will wait for the sub-thread to finish and will then finish last.

仅供参考:主线程在子线程之前完成不是问题.子线程不是守护线程,因此 JVM 会在 JVM 关闭之前等待它们完成.

FYI: it is not a problem to have the main thread finish before the sub-threads. The sub-threads are not daemon threads so the JVM will wait for them to complete before the JVM shuts down.

这篇关于如何让主端持续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆