线程等待join(),wait()

本文探讨了如何在多线程环境中使用join()和wait()方法来实现线程间的同步。场景描述中,线程3在启动后会并发执行线程1和2,并在自身运行5秒后等待它们结束。线程1运行8秒,线程2运行10秒。通过线程3调用wait()方法释放锁,使得线程1和2有机会执行并结束,最终由线程1和2唤醒线程3继续执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景1.有3线程个同时执行,怎样第3个线程等1和2线程退出之后,自己退出?

仔细想了下,join()可以阻塞当前线程

 Thread thread1 = new Thread() {
        private boolean isRunning = true;
        private int timer;

        @Override
        public void run() {
            while (isRunning) {
                timer++;
                if (timer == 8) {
                    isRunning = false;
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.w("thread1", "thread1>>" + "" + timer);
            }
        }
    };


    Thread thread2 = new Thread() {
        private boolean isRunning = true;
        private int timer;

        @Override
        public void run() {
            while (isRunning) {
                timer++;
                if (timer == 10) {
                    isRunning = false;
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.w("thread2", "thread2>>" + "" + timer);
            }
        }
    };

    Thread thread3 = new Thread() {
        private boolean isRunning = true;
        private int timer;

        @Override
        public void run() {
            synchronized (mLock) {
                thread1.start();
                thread2.start();

                while (isRunning) {
                    timer++;
                    if (timer == 5) {
                        isRunning = false;
                    }
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.w("thread3", "thread3>>" + timer);
                }

                try {
                    thread1.join();
                    thread2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.w("thread3", "thread3>>" + "退出...");
            }
        }
    };

thread3.start();线程3启动,则会启动线程1和线程2,此时3个线程并发运行,为了区别开来,这里线程3运行5s后等待1和2结束,而线程1的生命周期是8s,线程2的生命周期是10s!

再看一个主动释放锁的例子

private Object mLock = new Object();

    Thread thread1 = new Thread() {
        private boolean isRunning = true;
        private int timer;

        @Override
        public void run() {
            synchronized (mLock) {
                while (isRunning) {
                    timer++;
                    if (timer == 3) {
                        isRunning = false;
                    }
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.w("thread1", "thread1>>" + timer);
                }
                if (!thread2.isAlive()){
                    mLock.notify();
                }
            }
            Log.e("thread1", "thread1 退出");
        }
    };


    Thread thread2 = new Thread() {
        private boolean isRunning = true;
        private int timer;

        @Override
        public void run() {
            synchronized (mLock) {
                while (isRunning) {
                    timer++;
                    if (timer == 6) {
                        isRunning = false;
                    }
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.w("thread2", "thread2>>" + timer);
                }
                if (!thread1.isAlive()){
                    mLock.notify();
                }
            }
            Log.e("thread2", "thread2 退出");

        }
    };

    Thread thread3 = new Thread() {
        private boolean isRunning = true;
        private int timer;

        @Override
        public void run() {
            synchronized (mLock) {
                thread1.start();
                thread2.start();
                try {
                    mLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                while (isRunning) {
                    timer++;
                    if (timer == 5) {
                        isRunning = false;
                    }
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.w("thread3", "thread3>>" + timer);
                }
            }
            Log.e("thread3", "thread3 退出");
        }
    };

这里3会等其他线程执行完才开始执行

线程3启动的时候,会启动1和2,然后自己wait()释放了锁,那么1和2有了获取锁的机会,然后1和2都结束的时候去释放锁notify,然后线程3就从阻塞态到了运行态,往下执行!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值