功能需求如下:
一个理发店里有两张理发椅子,各有一个理发师(线程),还有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);
}
}
欢迎下方讨论,一起成长!