随笔 - 171  文章 - 0  评论 - 0  阅读 - 62466

多线程抢沙包游戏

幼儿园玩抢沙包游戏,共计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();
        }
    }
}
复制代码
posted on   zhengbiyu  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示