首页 > Java > java教程 > 正文

确定线程何时结束

霞舞
发布: 2024-11-23 08:36:01
转载
681人浏览过

1。检查线程是否完成的方法:

isalive()

  • 如果线程仍在运行则返回true;否则,返回 false。
  • 用于持续检查线程的状态。

加入()

  • 使调用该方法的线程等待,直到指定线程完成。
  • 有多种变体允许您定义最长等待时间。

2。使用 isalive() 的示例:

// verifica se as threads estão vivas
class mythread implements runnable {
    thread thrd;

    mythread(string name) {
        thrd = new thread(this, name);
        thrd.start(); // inicia a thread
    }

    public void run() {
        system.out.println(thrd.getname() + " starting.");
        try {
            for (int count = 0; count < 10; count++) {
                thread.sleep(400); // suspende a execução por 400ms
                system.out.println("in " + thrd.getname() + ", count is " + count);
            }
        } catch (interruptedexception exc) {
            system.out.println(thrd.getname() + " interrupted.");
        }
        system.out.println(thrd.getname() + " terminating.");
    }
}

class morethreads {
    public static void main(string[] args) {
        system.out.println("main thread starting.");
        mythread mt1 = new mythread("child #1");
        mythread mt2 = new mythread("child #2");
        mythread mt3 = new mythread("child #3");

        // verifica se as threads ainda estão vivas
        do {
            system.out.print(".");
            try {
                thread.sleep(100);
            } catch (interruptedexception exc) {
                system.out.println("main thread interrupted.");
            }
        } while (mt1.thrd.isalive() || mt2.thrd.isalive() || mt3.thrd.isalive());

        system.out.println("main thread ending.");
    }
}

登录后复制

重要要点:

  • 主线程不断检查 isalive() 直到所有子线程都完成。
  • 线程的执行是由 java 调度的,因此确切的输出可能会有所不同。

3。使用 join() 的示例:

// aguarda o término das threads com join()
class mythread implements runnable {
    thread thrd;

    mythread(string name) {
        thrd = new thread(this, name);
        thrd.start(); // inicia a thread
    }

    public void run() {
        system.out.println(thrd.getname() + " starting.");
        try {
            for (int count = 0; count < 10; count++) {
                thread.sleep(400); // suspende a execução por 400ms
                system.out.println("in " + thrd.getname() + ", count is " + count);
            }
        } catch (interruptedexception exc) {
            system.out.println(thrd.getname() + " interrupted.");
        }
        system.out.println(thrd.getname() + " terminating.");
    }
}

class jointhreads {
    public static void main(string[] args) {
        system.out.println("main thread starting.");
        mythread mt1 = new mythread("child #1");
        mythread mt2 = new mythread("child #2");
        mythread mt3 = new mythread("child #3");

        try {
            mt1.thrd.join(); // aguarda child #1 terminar
            system.out.println("child #1 joined.");
            mt2.thrd.join(); // aguarda child #2 terminar
            system.out.println("child #2 joined.");
            mt3.thrd.join(); // aguarda child #3 terminar
            system.out.println("child #3 joined.");
        } catch (interruptedexception exc) {
            system.out.println("main thread interrupted.");
        }

        system.out.println("main thread ending.");
    }
}

登录后复制

预期输出(可能会有所不同):

Main thread starting.
Child #1 starting.
Child #2 starting.
Child #3 starting.
In Child #1, count is 0
In Child #2, count is 0
In Child #3, count is 0
...
Child #1 terminating.
Child #1 joined.
Child #2 terminating.
Child #2 joined.
Child #3 terminating.
Child #3 joined.
Main thread ending.

登录后复制

重要要点:

  • join() 确保主线程仅在子线程完成后继续。
  • 输出显示每个线程按预期顺序终止。

确定线程何时结束

以上就是确定线程何时结束的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号