多线程 - java经典面试题:子线程先运行30次主线程,主线程40次,如此循环50次?
阿神
阿神 2017-04-18 10:23:30
[Java讨论组]

最近偶遇这道题,网上相似的题都是循环次数不一样。然而我百度搜到的论坛或者博客感觉都不太对,运行有穿插。请给出正确结果。
我们假使所有人都引入了业务对象。

并且我有疑问?感觉题目本意不是new Thread()放在前面。
网上有人做法是用标志位防止虚假唤醒,还有锁放在方法上的。是否有道理?

public class Test {

    public static void main(String[] args) throws InterruptedException {
        final Business business = new Business();
        // 子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        business.sonBusiness(i);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }).start();
        for (int i = 0; i < 50; i++) {
            business.mainBusiness(i);
        }

    }

}

class Business {
    public void mainBusiness(int i) throws InterruptedException {
        synchronized (this) {
            for (int j = 1; j <= 20; j++) {
                System.out.println("主线程第" + i + "轮,第" + j + "次");
            }
            this.notify();
            this.wait();
        }

    }

    public void sonBusiness(int i) throws InterruptedException {
        synchronized (this) {
            for (int j = 1; j <= 30; j++) {
                System.err.println("子线程第" + i + "轮,第" + j + "次");
            }
            this.notify();
            this.wait();
        }
    }
}

https://www.zhihu.com/questio...

阿神
阿神

闭关修行中......

全部回复(3)
PHP中文网

你的代码看起来没有问题,不过System.outerr是两个不同的流,可能是这两个流输出的时候有的问题吧。个人认为程序是按照你的想法跑了,可是输出却没有正确的输出吧。

高洛峰
public class Test {

    public static void main(String[] args) throws InterruptedException {
        final Business business = new Business();
        // 子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 50; i++) {
                    try {
                        business.sonBusiness(i);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }).start();
        
        for (int i = 1; i <= 50; i++) {
            business.mainBusiness(i);
        }

    }

}

class Business {
    private static Object object = new Object();
    public void mainBusiness(int i) throws InterruptedException {
        synchronized (object) {
            object.notify();
            for (int j = 1; j <= 20; j++) {
                System.out.println("主线程第" + i + "轮,第" + j + "次");
            }
            object.wait();
        }

    }

    public void sonBusiness(int i) throws InterruptedException {
        synchronized (object) {
            object.notify();
            for (int j = 1; j <= 30; j++) {
                System.err.println("子线程第" + i + "轮,第" + j + "次");
            }
            object.wait();
        }
    }
}

你用this也没关系,你这就是一个对象锁,所以不会出现多个对象多个锁的问题,你穿插的问题在于你的唤醒和等待写错了,不能又唤醒又等待,这样有什么意义。先唤醒执行代码,执行完再等待。
PHPz

知乎上不是有答案了么。
synchronized 是多个线程访问同一个方法时的锁,你这是两个线程访问两个方法,所以根本没起作用。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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