求 java 大神解答一道线程题 感恩

要求:使用多线程模拟医院排号叫号系统。(1)主线程中创建ArrayList集合维护排队等待服务的号码(2)加号线程(每1秒增加一个号码到等待服务的队列中去(增加一个号码到集合),队列中最多可排队10个号,达到10后要等待“叫号线程”叫号后才可继续增加)(3)叫号线程(每3秒从服务队列中叫一个号(从集合中移除目前最小的号)去对应诊室就诊,队列为空时候不可叫号要等待“加号线程”加号后即可继续叫号)(4)主线程中开启1个加号线程和2个叫号线程

1)加号线程类正确(13分)①线程通信相关代码正确(5分)②每1秒增加1个号码,输出“*号,进入排队状态”其中*号要显示具体的号码(5分)③该类完整代码正确(3分)(2)叫号线程类正确(13分) ①线程通信相关代码正确 (5分)②每3秒叫一个号码,输出“请第*号去*就诊”其中的*号要显示具体的号码或诊室名(5分)③该类完整代码正确(3分)(3)主线程正确启动1个加号线程类(1分)(4)主线程正确启动2个叫号线程类,线程名分别为“诊室1”和“诊室2”(叫号线程中的诊室名对应这个名称)(4分)(5)完整效果正确(5分)(6)不能出现重复叫号或漏号现象(4分)

 

 

  •  

package ThreadInterCommunication;

import java.util.ArrayList;

public class CallOrder {
    private static String message = "A";
    private static int count = 1;
    private static ArrayList al = new ArrayList();

    public static void main(String[] args) {

        Thread addOrder = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() < 11) {

                    System.out.println(count + "号,进入排队状态");
                    // System.out.println(al);
                    al.add(count);
                    count = count + 1;
                }
            }

        });

        Thread delOrder1 = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() > 0) {

                    // System.out.println(count);
                    // System.out.println(al);
                    System.out.println("请第" + al.remove(0) + "号去诊室1就诊");
                    // count = count + 1;
                }
            }

        });
        Thread delOrder2 = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() > 0) {

                    // System.out.println(count);
                    // System.out.println(al);
                    System.out.println("请第" + al.remove(0) + "号去诊室2就诊");
                    // count = count + 1;
                }
            }

        });

        addOrder.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        delOrder1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        delOrder2.start();
    }
}

package ThreadInterCommunication;

import java.util.ArrayList;

public class CallOrder {

    private static int count = 1;
    private static ArrayList<Integer> al = new ArrayList<Integer>();

    public static void main(String[] args) {

        Thread addOrder = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() < 11) {

                    System.out.println(count + "号,进入排队状态");
                    // System.out.println(al);
                    al.add(count);
                    count = count + 1;
                }
            }

        });

        Thread delOrder1 = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() > 0) {

                    // System.out.println(count);
                    // System.out.println(al);
                    System.out.println("请第" + al.remove(0) + "号去诊室1就诊");
                    // count = count + 1;
                }
            }

        });
        Thread delOrder2 = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() > 0) {

                    // System.out.println(count);
                    // System.out.println(al);
                    System.out.println("请第" + al.remove(0) + "号去诊室2就诊");
                    // count = count + 1;
                }
            }

        });

        addOrder.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        delOrder1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        delOrder2.start();
    }
}

1号,进入排队状态
2号,进入排队状态
3号,进入排队状态
请第1号去诊室1就诊
4号,进入排队状态
请第2号去诊室2就诊
5号,进入排队状态
6号,进入排队状态
请第3号去诊室1就诊
7号,进入排队状态
请第4号去诊室2就诊
8号,进入排队状态
9号,进入排队状态
请第5号去诊室1就诊
10号,进入排队状态
请第6号去诊室2就诊
11号,进入排队状态
12号,进入排队状态
请第7号去诊室1就诊
13号,进入排队状态
请第8号去诊室2就诊
14号,进入排队状态
15号,进入排队状态
请第9号去诊室1就诊
16号,进入排队状态
请第10号去诊室2就诊
17号,进入排队状态
18号,进入排队状态
请第11号去诊室1就诊
19号,进入排队状态
请第12号去诊室2就诊
20号,进入排队状态

package ThreadInterCommunication;

import java.util.ArrayList;

public class CallOrder2 {

    private static volatile int count = 1;
    private static volatile ArrayList<Integer> al = new ArrayList<Integer>();

    public static void main(String[] args) {

        Thread addOrder = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() < 11) {

                    System.out.println(count + "号,进入排队状态");
                    // System.out.println(al);
                    al.add(count);
                    count = count + 1;
                }
            }

        });

        Thread delOrder1 = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() > 0) {

                    // System.out.println(count);
                    // System.out.println(al);
                    System.out.println("请第" + al.remove(0) + "号去诊室1就诊");
                    // count = count + 1;
                }
            }

        });
        Thread delOrder2 = new Thread(() -> {

            while (1 > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (al.size() > 0) {

                    // System.out.println(count);
                    // System.out.println(al);
                    System.out.println("请第" + al.remove(0) + "号去诊室2就诊");
                    // count = count + 1;
                }
            }

        });

        addOrder.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        delOrder1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        delOrder2.start();
    }
}