多线程抢沙包游戏
幼儿园玩抢沙包游戏,共计100个沙包,有10个小朋友(4男6女),男生每次拿3个沙包,女生每次拿2个沙包,如果剩余的沙包不够每次拿的数量,则游戏停止,请用java多线程模拟上述游戏过程。
import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 幼儿园玩抢沙包游戏,共计100个沙包,有10个小朋友(4男6女),男生每次拿3个沙包,女生每次拿2个沙包, * 如果剩余的沙包不够每次拿的数量,则游戏停止,请用java多线程模拟上述游戏过程。 */ public class SnatchingSandbags { private static volatile int total = 100; private static Lock l = new ReentrantLock(); private static CountDownLatch countDownLatch = new CountDownLatch(10); public static void main(String[] args) { BoyRunLock boy = new BoyRunLock(); GirlRunLock girl = new GirlRunLock(); List<Thread> threadList= new ArrayList<>(); for (int i = 0; i < 4; i++) { threadList.add(new Thread(boy, "boy" + (i + 1))); } for (int i = 0; i < 6; i++) { threadList.add(new Thread(girl, "girl" + (i + 1))); } for (Thread thread : threadList) { thread.start(); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(total); //以下代码测试tryLock,可以看出tryLock能让更多小朋友拿到沙袋 total = 100; CountDownLatch countDownLatch = new CountDownLatch(10); BoyRun boyRun = new BoyRun(); GirlRun girlRun = new GirlRun(); threadList= new ArrayList<>(); for (int i = 0; i < 4; i++) { threadList.add(new Thread(boyRun, "boy" + (i + 1))); } for (int i = 0; i < 6; i++) { threadList.add(new Thread(girlRun, "girl" + (i + 1))); } for (Thread thread : threadList) { thread.start(); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(total); } static class BoyRun implements Runnable { @Override public void run() { while (total >= 3) { if (l.tryLock()) { if (total >= 3) { System.out.println("threadName:" + Thread.currentThread().getName() + "BoyRun current total:" + total + ",after total:" + (total -= 3)); } l.unlock(); } } System.out.println("BoyRun end"); countDownLatch.countDown(); } } static class BoyRunLock implements Runnable { @Override public void run() { while (total >= 3) { l.lock(); if (total >= 3) { System.out.println("threadName:" + Thread.currentThread().getName() + "BoyRun current total:" + total + ",after total:" + (total -= 3)); } l.unlock(); } System.out.println("BoyRun end"); countDownLatch.countDown(); } } static class GirlRun implements Runnable { @Override public void run() { while (total >= 2) { if (l.tryLock()) { if (total >= 2) { System.out.println("threadName:" + Thread.currentThread().getName() + "GirlRun current total:" + total + ",after total:" + (total -= 2)); } l.unlock(); } } System.out.println("GirlRun end"); countDownLatch.countDown(); } } static class GirlRunLock implements Runnable { @Override public void run() { while (total >= 2) { l.lock(); if (total >= 2) { System.out.println("threadName:" + Thread.currentThread().getName() + "GirlRun current total:" + total + ",after total:" + (total -= 2)); } l.unlock(); } System.out.println("GirlRun end"); countDownLatch.countDown(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构