Thread基本功能解析

start

// 同步方法
 public synchronized void start() {
// 检查线程状态
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

//添加到指定线程组,thread默认使用调用线程的线程组
        group.add(this);
        boolean started = false;
        try {
//本地方法启动线程
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
//开启失败则由线程组处理,从组内移除,并进行计数
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

join

//所有join方法的重载最终都会进入此方法,入参为0时代表永久等待,原始语义为等待本线程死亡直到x秒
//isAlive方法由查看eetop值判断
public final synchronized void join(final long millis)
    throws InterruptedException {
//有时间限制
        if (millis > 0) {
            if (isAlive()) {
                final long startTime = System.nanoTime();
                long delay = millis;
                do {
//直接使用wait方法
                    wait(delay);
                } while (isAlive() && (delay = millis -
                        TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)) > 0);
            }
        } else if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            throw new IllegalArgumentException("timeout value is negative");
        }
    }
posted @   hwh405  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示