多线程控制数字的加减:
线程控制数字的加减过程应该是一个加一个减,这个和消费者模型有点像,加了后再减,加减不同同时进行,所以存在同步的问题。
/* 定义一个操作资源 * 这个类用来创建数值和加减切换开关还有加减的操作 * 其它线程类则创建这个类的属性来进行关联,并调用这个类中的方法实现加减的操作。 * */ public class Resource { private int num = 0; // 进行加减操作的数据 private boolean flag =true; // 加减的切换 // flag = true : 表示可以进行加法操作,不能减法操作 // flag = false : 表示可以进行减法操作,不能加法操作 public synchronized void add(){ // 加法操作,已同步 if (this.flag == false){ // 现在需要执行的是减法操作,加法操作需要等待 try { super.wait(); // super 表示父类,wait()是Object的方法所以super指向的是Object } catch (InterruptedException e) { e.printStackTrace(); } } //延迟 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } this.num ++; System.out.println("【加法操作】- " + Thread.currentThread().getName() + ": num = " + this.num); // 因为flag = true执行加法,所以将flag设置为false执行减法 this.flag = false; // 加法操作执行完毕,需要进行减法操作 // super 表示父类,notifyAll()是Object的方法所以super指向的是Object super.notifyAll(); // 等待的可能是加法或者减法,那么就唤醒全部线程 } public synchronized void sub(){ // 减法操作,已同步 if (this.flag == true){ // 现在需要执行的是加法操作,减法操作需要等待 try { super.wait(); // super 表示父类,wait()是Object的方法所以super指向的是Object } catch (InterruptedException e) { e.printStackTrace(); } } //延迟 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } this.num --; System.out.println("【减法操作】- " + Thread.currentThread().getName() + ": num = " + this.num); this.flag = true; // true:执行加法 super.notifyAll(); // super 表示父类,notifyAll()是Object的方法所以super指向的是Object } }
// 加法线程 public class AddThread implements Runnable{ private Resource resource; public AddThread(Resource resource){ this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { this.resource.add(); } } }
// 减法线程 public class SubThread implements Runnable{ private Resource resource; public SubThread(Resource resource){ this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { this.resource.sub(); } } }
// 客户端 public class Main { public static void main(String[] args) { Resource res = new Resource(); //创建资源类对象 AddThread at = new AddThread(res); //创建加法线程类对象 SubThread st = new SubThread(res); //创建减法线程类对象 new Thread(at,"加法线程 - A").start(); new Thread(at,"加法线程 - B").start(); new Thread(st,"减法线程 - A").start(); new Thread(st,"减法线程 - B").start(); } }
输出结果:
num的值最终为0,加减法的交替进行得以验证,但是因为线程优先级的问题,无法保证某一个方法的某个线程先执行。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)