阿里 多线程 java

阿里多线程题,请各位大佬斧正

题目:
一个浴室有3个淋浴间,现在有20人需要洗澡,男女若干。要求模拟20个线程,随机去浴室洗澡,确保每个人都洗澡了,并且避免男女同时使用浴室,每个人洗澡的时间为一个随机数。实现代码。

import java.util.Random;
import java.util.concurrent.Semaphore;

public class Main {

    static Semaphore semaphore = new Semaphore(3);
    volatile static boolean flagMan = false;
    volatile static boolean flagWoman = false;
    final static Object lockMan = new Object();
    final static Object lockWoman = new Object();
    static Random random = new Random();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            Thread.sleep(random.nextInt(2000));
            if(random.nextInt(2)==0){
                funMan(i);
            }else{
                funWoman(i);
            }
        }
    }

    private static void funWoman(int finalI) {
        new Thread(()->{
            try{
                flagWoman=true; // 用于新线程
                if(flagMan){
                    synchronized (lockWoman){
                        lockWoman.wait();
                    }
                }
                flagWoman=true; // 用于旧线程

                semaphore.acquire();
                int time = random.nextInt(2000);
                System.out.println("女.."+finalI+"..开始洗澡,要洗"+ time+ "毫秒");
                Thread.sleep(time);
                System.out.println("女.."+finalI+"..结束洗澡");
                semaphore.release();

                synchronized (lockMan){
                    if(semaphore.availablePermits()==3){
                        flagWoman=false;
                        lockMan.notifyAll();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "女...").start();
    }

    private static void funMan(int finalI) {
        new Thread(()->{
            try{
                flagMan = true; // 用于新线程
                if(flagWoman){
                    synchronized (lockMan){
                        lockMan.wait();
                    }
                }
                flagMan = true; // 用于旧线程


                semaphore.acquire();
                int time = random.nextInt(2000);
                System.out.println("男.."+finalI+"..开始洗澡,要洗"+ time+ "毫秒");
                Thread.sleep(time);
                System.out.println("男.."+finalI+"..结束洗澡");
                semaphore.release();

                synchronized (lockWoman){
                    if(semaphore.availablePermits()==3){
                        flagMan = false;
                        lockWoman.notifyAll();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "男...").start();
    }
}

output:
男..0..开始洗澡,要洗1796毫秒
男..0..结束洗澡
女..2..开始洗澡,要洗1594毫秒
女..1..开始洗澡,要洗1132毫秒
女..1..结束洗澡
女..2..结束洗澡
男..3..开始洗澡,要洗477毫秒
男..4..开始洗澡,要洗846毫秒
男..3..结束洗澡
男..4..结束洗澡
男..5..开始洗澡,要洗555毫秒
男..5..结束洗澡
男..6..开始洗澡,要洗1653毫秒
男..6..结束洗澡
女..7..开始洗澡,要洗1448毫秒
女..8..开始洗澡,要洗281毫秒
女..8..结束洗澡
女..9..开始洗澡,要洗893毫秒
女..9..结束洗澡
女..7..结束洗澡
男..10..开始洗澡,要洗447毫秒
男..10..结束洗澡
女..11..开始洗澡,要洗1638毫秒
女..12..开始洗澡,要洗297毫秒
女..12..结束洗澡
女..13..开始洗澡,要洗1871毫秒
女..11..结束洗澡
女..13..结束洗澡
男..14..开始洗澡,要洗1193毫秒
男..17..开始洗澡,要洗1649毫秒
男..18..开始洗澡,要洗266毫秒
男..18..结束洗澡
男..14..结束洗澡
男..17..结束洗澡
女..16..开始洗澡,要洗510毫秒
女..15..开始洗澡,要洗1937毫秒
女..16..结束洗澡
女..15..结束洗澡
男..19..开始洗澡,要洗1758毫秒
男..19..结束洗澡

#笔试题目##阿里巴巴#
全部评论
兄弟,照你这样洗法得洗一年吧,你得把20个人安排妥了。
点赞 回复
分享
发布于 2020-12-30 09:16

相关推荐

点赞 5 评论
分享
牛客网
牛客企业服务