Java多线程 理发店小测试

功能需求如下:
一个理发店里有两张理发椅子,各有一个理发师(线程),还有5张用户椅子。

初始主线程每间隔100毫秒产生一个用户(线程),连续产生20名用户。
一个用户理发需要300毫秒。
用户线程的逻辑:
1)如果理发店客满(两个理发师正忙理发,5张客户椅子有人坐着等待),理发店外的用户(线程),将在外等待。
2)但理发店中一张用户椅子空,则允许一个用户进入并坐上椅子等待。
3)但一个用户理完发,允许5张椅子上的某个用户获得理发,并释放该用户占据的用户椅子。
显示每个用户的状态,直到所有20名用户理完发
将用户状态信息写入文件record.txt中

 

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Result {
    static  List<String> list = Collections.synchronizedList(new ArrayList<>(20)); //获取线程安全20个客户
    static List<String> bench = Collections.synchronizedList(new ArrayList<>(5)); //获取线程安全板凳1
    static  int client=1;//  客户

    public static void main(String[] args) {

        try {
            abc();  //将输出语句改成到文件里面去
        } catch (Exception e) {
            e.printStackTrace();
        }



        Thread th=new Thread(new Thy());  //产生用户
        th.start();


        Thread lfs_add=new Thread(() -> {
            while(true){
                synchronized (Result.class){
                    if (bench.size()<5) {
                        if(list.size()!=0){
                            try{
                                String s = list.get(0); //取出
                                System.out.println("2>>"+Thread.currentThread().getName() + "  添加了客户:<"+s+">  当前等待客户有"+(bench.size()+1)+"个");
                                list.remove(0); //删除
                                bench.add(s);
                            }catch (Exception e){
                                System.out.println(Thread.currentThread().getName()+"已经没有用户");

                            }


                        }else{
                            if(!th.isAlive()){
                                System.out.println("结束了线程");
                                break;  //结束线程
                            }

                        }
                    }

                }
            }
        });
        lfs_add.setName("板凳");
        lfs_add.start(); //启动线程



        ///理发师



        Thread th2=new Thread(() -> {   //理发师1

            while (true) {
                synchronized (Result.class)
                {
                    if (bench.size() == 0) { //如果没有用户
                        if (!lfs_add.isAlive()) {
                            System.out.println(Thread.currentThread().getName() + "结束了自己的工作");
                            break;
                        }


                        continue;
                    }
                    String s = bench.get(0); //取出
                    System.out.println("3>>((((" + Thread.currentThread().getName() + "正在理:" + s + "))))");
                    bench.remove(0); //删除


                    try {
                        Thread.sleep(300); //300毫秒理发
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("3>>((((" + Thread.currentThread().getName() + "理完了:" + s + "))))");
                }
            }

        });
        th2.setName("理发师1");
        th2.start(); //启动线程



        Thread th3=new Thread(() -> {   //理发师2

            while (true) {
                synchronized (Result.class) {
                    if (bench.size() == 0) { //如果没有用户
                        if (!lfs_add.isAlive()) {
                            System.out.println(Thread.currentThread().getName() + "结束了自己的工作");
                            break;
                        }


                        continue;
                    }
                    String s = bench.get(0); //取出
                    System.out.println("3>>((((" + Thread.currentThread().getName() + "正在理:" + s + "))))");
                    bench.remove(0); //删除


                    try {
                        Thread.sleep(300); //300毫秒理发
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("3>>((((" + Thread.currentThread().getName() + "理完了:" + s + "))))");
                }

            }



        });
        th3.setName("理发师2");
        th3.start(); //启动线程



    }


    public static void abc() throws Exception {  //让输出语句保存到文件里面
        FileSystemView view = FileSystemView.getFileSystemView();
        File file = view.getHomeDirectory();
        FileOutputStream fos = new FileOutputStream(file.getPath()+"\\record.txt"); //桌面路径
        PrintStream ps = new PrintStream(fos, false);//true代表覆盖操作
        System.setOut(ps); //修改打印流吧打印出去的地方修改成输入到文件里面


    }




}

class Thy extends Thread{

    @Override
    public void run() {
        do {

            System.out.println("1>>" + "进来了一个客户" + Result.client);
            Result.list.add("客户" + Result.client);


            Result.client++;
            try {

                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } while (Result.client <= 20);
    }
}

欢迎下方讨论,一起成长!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不被定义的~wolf

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

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

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

打赏作者

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

抵扣说明:

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

余额充值