Java多线程开发——一个简单的数字加减线程阻塞导致的数字一直加或一直减的解决办法

范例一:

package cn.mldn.cn;
public class TeacherDemo {
    public static void main(String[] args){
        Resource res = new Resource() ;
        TeacherSubThread st = new TeacherSubThread(res) ;
        TeacherAddThread at = new TeacherAddThread(res) ;
        new Thread(at,"加法线程 - A").start() ;
       // new Thread(at,"加法线程 - B").start() ;
        new Thread(st,"减法线程 - X").start() ;
        //new Thread(st,"减法线程 - Y").start() ;
    }
}
class Resource {   // 定义一个操作的资源
    private int num = 0 ; // 这个要进行加减操作的数据
    private boolean flag = true ; // 加减的切换
    // flag = true:表示可以进行加法操作,但是无法进行减法操作;
    // flag = false:表示可以进行减法操作,但是无法进行加法操作。
    public synchronized void add() throws Exception { // 执行加法操作
        if (!this.flag) {  // 现在需要执行的是减法操作,加法操作要等待
            super.wait();
        }
        else {
            Thread.sleep(100);
            this.num++;
            System.out.println("【加法操作 - " + Thread.currentThread().getName() + "】num = " + this.num);
            this.flag = false ; // 加法操作执行完毕,需要执行减法处理
            super.notifyAll(); // 唤醒全部等待线程
        }
    }
    public synchronized void sub() throws Exception { // 执行减法操作
        if (this.flag) {   // 减法操作需要等待
            super.wait();
        }else{
            Thread.sleep(100);
            this.num--;
            System.out.println("【减法操作 - " + Thread.currentThread().getName() + "】num = " + this.num);
            this.flag = true ;
            super.notifyAll();
        }
    }
}
class TeacherAddThread implements Runnable {
    private final Resource resource ;
    public TeacherAddThread(Resource resource) {
        this.resource = resource ;
    }
    @Override
    public void run() {
        for (int x = 0 ; x < 50 ; x ++) {
            try {
                this.resource.add() ;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
class TeacherSubThread implements Runnable {
    private final Resource resource ;
    public TeacherSubThread(Resource resource) {
        this.resource = resource ;
    }
    @Override
    public void run() {
        for (int x = 0 ; x < 50 ; x ++) {
            try {
                this.resource.sub() ;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

这个主要是在if之后再加else进行判断

范例二:

package cn.mldn.cn;
public class CaseDemo {
    public static void main(String[] args) {
        Resources res = new Resources();
        Thread thread1 = new Thread(new AddThread(res), "加法线程- A");
        Thread thread2 = new Thread(new AddThread(res),"加法线程- B");
        Thread thread3 = new Thread(new SubThread(res),"减法线程- C");
        Thread thread4 = new Thread(new SubThread(res),"减法线程- D");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
class Resources{
    private int num = 0 ;
    private boolean flag = true;
    public synchronized void add() throws Exception{
        while (!this.flag) {
            super.wait();
        }
       Thread.sleep(100);
        this.num++;
        System.out.println("【加法操作 - " + Thread.currentThread().getName() +"】num" + this.num);
        this.flag = false;
        super.notifyAll();
    }
    public synchronized void sub() throws Exception{
        while (this.flag) {
            super.wait();
        }
        Thread.sleep(200);
        this.num--;
        System.out.println("【减法操作 - " + Thread.currentThread().getName() + "】num" + this.num);
        this.flag = true;
        super.notifyAll();
    }
}
class AddThread implements Runnable{
    private final Resources resources;
    public AddThread ( Resources resources){
        this.resources = resources;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x = 0 ; x < 50 ; x ++){
            try {
                this.resources.add();
            } catch (Exception e) {
                e.printStackTrace();
                //TODO: handle exception
            }
        }
    }
}
class SubThread implements Runnable{
    private final Resources resources;
    public SubThread(Resources resources){
        this.resources = resources;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x = 0 ; x < 50 ; x ++){
            try {
                this.resources.sub();
            } catch (Exception e) {
                e.printStackTrace();
                //TODO: handle exception
            }
        }
    }
}

这是将if改成while,执行前先进行判断

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值