Java每日一练11根据年月日获取星期数,哲学家进食问题

本文介绍了如何在Java中根据年月日计算星期数,并探讨了多线程环境下哲学家进食问题的解决方案,涉及并发控制与线程同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据年月日获取日期数

/**
 * 一周中的第几天
 *
 * @author 驳壳毛瑟
 * @apiNote of(int year, Month month, int dayOfMonth)
 * 从一年,一个月和一天获得一个 LocalDate的实例
 * <p>
 * getDayOfWeek()
 * 获取星期几字段,这是一个枚举 DayOfWeek
 * <p>
 * getValue()
 * 枚举类DayOfWeek方法,获得星期几 int价值
 */
class Solution5 {
    public String dayOfTheWeek(int day, int month, int year) {
        LocalDate localDate = LocalDate.of(year, month, day);
        String[] ss = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
        return ss[localDate.getDayOfWeek().getValue() - 1];
    }
}

class LocalDateTest {
    public static void main(String[] args) {
        Solution5 solution5 = new Solution5();
        String s = solution5.dayOfTheWeek(3, 1, 2022);
        System.out.println(s); // Monday
    }
}

多线程哲学家进食问题

class DinnerExecutor {
    static final Semaphore fork1 = new Semaphore(1);
    static final Semaphore fork2 = new Semaphore(1);
    static final Semaphore fork3 = new Semaphore(1);
    static final Semaphore fork4 = new Semaphore(1);
    static final Semaphore fork5 = new Semaphore(1);
    // 防止死锁的发生,给了4个控制权
    static final Semaphore forkControl = new Semaphore(4);

    public static void main(String[] args) {
        new Thread(new Philosopher(1, fork5, fork1)).start();
        new Thread(new Philosopher(2, fork1, fork2)).start();
        new Thread(new Philosopher(3, fork2, fork3)).start();
        new Thread(new Philosopher(4, fork3, fork4)).start();
        new Thread(new Philosopher(5, fork4, fork5)).start();
    }

    static class Philosopher implements Runnable {
        int no;             // 哲学家编号
        Semaphore left;     // 哲学家左边的叉子
        Semaphore right;    // 哲学家右边的叉子

        public Philosopher(int no, Semaphore left, Semaphore right) {
            this.no = no;
            this.left = left;
            this.right = right;
        }

        public void run() {
            try {
                // 这是饭卡许可证,为了防止死锁的发生,有一位哲学家得不到吃饭权
                forkControl.acquire();
                boolean leftFirst = new Random().nextInt(2) % 2 == 0; // true,false
                // 计算叉子坐标
                int dot = (leftFirst ? no - 1 : no); // no - 1左,no右
                if (dot == 0) dot = 5; // 按顺时针放叉子的位置的话,坐标5就是编号为1哲学家叉子的左边
                // 预分配叉子的使用权,剩下的全靠哲学家抢占时间片的速度来决定哪位哲学家拿到了叉子
                    System.out.println(no + "拿到了吃饭卡,并且准备去拿" + (leftFirst ? "左边" : "右边") + "叉子=" + dot);
                if (leftFirst) {
                    left.acquire(); // 拿走叉子的使用权
                } else {
                    right.acquire(); // 拿走叉子的使用权
                }
                    System.out.println(no + "拿到了" + (leftFirst ? "左边" : "右边") + "叉子=" + dot);
                // 线程sleep方法,下面抢占叉子的概率是等可能的
                Thread.sleep(1000);
                // 哲学家们再次枪占叉子
                if (leftFirst) {
                    right.acquire();
                } else {
                    left.acquire();
                }
                int dot2 = (leftFirst ? no : no - 1);
                if (dot2 == 0) dot2 = 5;
                    System.out.println(no + "也拿到了另一边叉子=" + dot2);
                    System.out.println(this.no + "号哲学家在吃饭");
                // 下一次抢占也是等可能的
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // 程序运行到此处
                // 一位哲学家成功进食并且放下了叉子
                // 正常的话,这里一共会被执行5次
                right.release();
                left.release();
                System.out.println(no + "释放了所有叉子");
                forkControl.release();
                System.out.println(no + "释放了吃饭卡");
                System.out.println(this.no + "号哲学家在休息");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

muskfans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值