首页 > Java > java教程 > 正文

Java 多线程加减交替运算为何结果与预期不符?

霞舞
发布: 2024-11-03 09:46:01
原创
1117人浏览过

java 多线程加减交替运算为何结果与预期不符?

java 多线程实现加减交替运算 结果与预期不符

在给出的 java 代码中,作者使用多线程尝试实现加减交替运算,但运行时出现结果与预期不符的问题。代码如下:

class resource {
    // 定义一个操作的资源
    private int num = 0;        // 定义要进行加减操作的数据
    private boolean flag = true;  // flag = true 表示可以进行加法操作,无法进行减法操作
                                 // flag = false 表示可以进行减法操作,无法进行加法操作

    public synchronized void add() throws interruptedexception {
        // 现在需要执行的是减法操作,加法操作需要等待
        if (!this.flag) {
            super.wait();
        }
        thread.sleep(100);
        this.num++;
        system.out.println("[加法操作-" + thread.currentthread().getname() + "]num = " + this.num+ " " + this.flag);
        this.flag = false;
        super.notifyall();
    }

    public synchronized void sub() throws interruptedexception {
        // 现在进行的是减法操作,加法操作需要等待
        if (this.flag) {
            super.wait();
        }
        thread.sleep(200);
        this.num--;
        system.out.println("[减法操作-" + thread.currentthread().getname() + "]num = " + this.num + " " + this.flag);
        this.flag = true;
        super.notifyall();
    }
}

class addthread implements runnable {
    private resource resource;

    public addthread(resource resource) {
        this.resource = resource;
    }

    @override
    public void run() {
        for (int i = 0; i < 50; i++) {
            try {
                this.resource.add();
            } catch (interruptedexception e) {
                e.printstacktrace();
            }
        }
    }
}

class subthread implements runnable {
    private resource resource;

    public subthread(resource resource) {
        this.resource = resource;
    }

    @override
    public void run() {
        for (int i = 0; i < 50; i++) {
            try {
                this.resource.sub();
            } catch (interruptedexception e) {
                e.printstacktrace();
            }
        }
    }
}

public class mycalculation {
    public static void main(string[] args) {
        resource resource = new resource();
        addthread addthread = new addthread(resource);
        subthread subthread = new subthread(resource);
        new thread(addthread, "加法线程a").start();
        new thread(addthread, "加法线程b").start();
        new thread(subthread, "减法线程a").start();
        new thread(subthread, "减法线程b").start();
    }
}
登录后复制

问题在于,多个线程同时进入 add 或 sub 方法后,可能会出现竞争条件。例如,当 sub 线程进入方法后,如果 flag 为 true,线程将进入 wait() 状态。此时,add 线程可以成功进入方法,将 num 加 1。然后,add 线程将 flag 设置为 false,并唤醒所有等待的线程。但此时,sub 线程仍处于 wait 状态,无法继续执行。当 sub 线程被唤醒后,它将继续执行,并可能将 num 减 1。这样,最终结果可能与预期不符,导致 num 出现负值。

为了解决这个问题,java 中提供了 wait(long) 和 notifyall(long) 方法,可以指定等待时间。通过设置等待时间,可以防止线程处于 wait 状态过久,从而避免出现竞争条件。

立即学习Java免费学习笔记(深入)”;

修改后的代码如下:

public synchronized void add() throws interruptedexception {
    // 现在需要执行的是减法操作,加法操作需要等待
    while (!this.flag) {
        super.wait(10);
    }
    thread.sleep(100);
    this.num++;
    system.out.println("[加法操作-" + thread.currentthread().getname() + "]num = " + this.num+ " " + this.flag);
    this.flag = false;
    super.notifyall(10);
}
登录后复制
public synchronized void sub() throws InterruptedException {
    // 现在进行的是减法操作,加法操作需要等待
    while (this.flag) {
        super.wait(10);
    }
    Thread.sleep(200);
    this.num--;
    System.out.println("[减法操作-" + Thread.currentThread().getName() + "]num = " + this.num + " " + this.flag);
    this.flag = true;
    super.notifyAll(10);
}
登录后复制

以上就是Java 多线程加减交替运算为何结果与预期不符?的详细内容,更多请关注php中文网其它相关文章!

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

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

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

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