其他分享
首页 > 其他分享> > 员工前后门入场,并统计次数

员工前后门入场,并统计次数

作者:互联网

员工前后门入场,并统计次数

某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票,假设公司有100个员工,

利用多线程模拟年会入场过程,并分别统计每个入口入场的人数,以及每个员工拿到的彩票的号码。线程运行后打印

格式如下:

编号为: 2 的员工 从后门 入场! 拿到的双色球彩票号码是:[17, 24, 29, 30, 31, 32, 07] 
编号为: 1 的员工 从后门 入场! 拿到的双色球彩票号码是:[06, 11, 14, 22, 29, 32, 15] 
//..... 
从后门入场的员工总共: 13 位员工 
从前门入场的员工总共: 87 位员工

题目中用到的产生双色球的工具类已经写好,可以直接使用

import java.util.Arrays;
import java.util.Random;

public class DoubleColorBallUtil {
    public static String create() {
        String[] red = {"01","02","03","04","05","06","07","08","09","10",
                "11","12","13","14","15","16","17","18","19","20","21","22","23",
                "24","25","26","27","28","29","30","31","32","33"};
        //创建蓝球
        String[] blue = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16".split(",");
        boolean[] used = new boolean[red.length];
        Random r = new Random();
        String[] all = new String[7];
        for(int i = 0;i<6;i++) {
            int idx;
            do {
                idx = r.nextInt(red.length);//0-32
            } while (used[idx]);//如果使用了继续找下一个
            used[idx] = true;//标记使用了
            all[i] = red[idx];//取出一个未使用的红球
        }
        all[all.length-1] = blue[r.nextInt(blue.length)];
        Arrays.sort(all);
        return Arrays.toString(all);
    }
}

测试类:

public class Test07 {
    public static void main(String[] args) {
        Staff staff = new Staff();

        new Thread(staff, "前门").start();
        new Thread(staff, "后门").start();
    }
}

线程类:

class Staff implements Runnable {
    //员工人数100人
    private int num = 100;


    @Override
    public void run() {
        String colorBall;//双色球变量
        int count = 0;//统计数量
        String name = Thread.currentThread().getName();

        while (true) {

            synchronized (this) {

                if (num > 0) {
                    colorBall = DoubleColorBallUtil.create();
                    System.out.println("编号为:" + num + "的员工从" + name + "入场!拿到双色球的号码是:" + colorBall);

                    num--;//进一个员工,少一个员工
                    count++;//计数加一

                    //休眠
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } else {
                    System.out.println("从"+name+"入场的员工共有:" + count + "位员工");
                    break;
                }
            }
        }

    }

}

打印结果:

编号为:100的员工从前门入场!拿到双色球的号码是:[02, 03, 05, 10, 21, 23, 25]
编号为:99的员工从前门入场!拿到双色球的号码是:[01, 03, 04, 16, 25, 31, 32]
编号为:98的员工从前门入场!拿到双色球的号码是:[01, 04, 07, 16, 17, 29, 30]
编号为:97的员工从前门入场!拿到双色球的号码是:[02, 10, 16, 21, 22, 23, 24]
编号为:96的员工从前门入场!拿到双色球的号码是:[04, 09, 12, 14, 17, 27, 28]
编号为:95的员工从前门入场!拿到双色球的号码是:[01, 02, 07, 13, 15, 21, 23]
编号为:94的员工从前门入场!拿到双色球的号码是:[01, 05, 07, 09, 16, 28, 32]
编号为:93的员工从前门入场!拿到双色球的号码是:[04, 05, 10, 14, 21, 29, 30]
编号为:92的员工从前门入场!拿到双色球的号码是:[04, 05, 06, 11, 17, 21, 23]
编号为:91的员工从前门入场!拿到双色球的号码是:[02, 09, 11, 16, 19, 23, 30]
编号为:90的员工从后门入场!拿到双色球的号码是:[01, 05, 11, 12, 19, 29, 33]
编号为:89的员工从后门入场!拿到双色球的号码是:[03, 07, 10, 10, 18, 23, 24]
编号为:88的员工从后门入场!拿到双色球的号码是:[02, 03, 05, 13, 16, 27, 31]
编号为:87的员工从后门入场!拿到双色球的号码是:[05, 08, 10, 12, 21, 26, 32]
编号为:86的员工从后门入场!拿到双色球的号码是:[01, 06, 06, 08, 16, 27, 31]
编号为:85的员工从后门入场!拿到双色球的号码是:[01, 02, 03, 04, 06, 14, 26]
编号为:84的员工从后门入场!拿到双色球的号码是:[05, 07, 15, 23, 27, 30, 32]
编号为:83的员工从后门入场!拿到双色球的号码是:[10, 12, 20, 25, 27, 28, 31]
编号为:82的员工从后门入场!拿到双色球的号码是:[01, 04, 12, 15, 16, 23, 30]
编号为:81的员工从后门入场!拿到双色球的号码是:[08, 09, 10, 10, 15, 17, 31]
编号为:80的员工从后门入场!拿到双色球的号码是:[02, 13, 19, 22, 23, 28, 32]
编号为:79的员工从后门入场!拿到双色球的号码是:[05, 10, 14, 20, 23, 24, 33]
编号为:78的员工从后门入场!拿到双色球的号码是:[03, 05, 10, 15, 26, 28, 30]
编号为:77的员工从后门入场!拿到双色球的号码是:[03, 04, 09, 18, 19, 22, 30]
编号为:76的员工从后门入场!拿到双色球的号码是:[01, 01, 09, 19, 27, 31, 32]
编号为:75的员工从后门入场!拿到双色球的号码是:[02, 05, 07, 10, 19, 27, 28]
编号为:74的员工从后门入场!拿到双色球的号码是:[05, 07, 07, 15, 23, 26, 31]
编号为:73的员工从后门入场!拿到双色球的号码是:[05, 08, 14, 14, 15, 24, 27]
编号为:72的员工从后门入场!拿到双色球的号码是:[05, 14, 21, 22, 31, 32, 33]
编号为:71的员工从后门入场!拿到双色球的号码是:[02, 03, 04, 06, 21, 28, 29]
编号为:70的员工从后门入场!拿到双色球的号码是:[04, 05, 08, 20, 23, 27, 33]
编号为:69的员工从后门入场!拿到双色球的号码是:[04, 07, 12, 15, 16, 19, 20]
编号为:68的员工从后门入场!拿到双色球的号码是:[01, 05, 11, 26, 31, 32, 33]
编号为:67的员工从后门入场!拿到双色球的号码是:[04, 06, 07, 15, 16, 23, 24]
编号为:66的员工从后门入场!拿到双色球的号码是:[06, 08, 09, 11, 21, 26, 31]
编号为:65的员工从后门入场!拿到双色球的号码是:[02, 07, 14, 15, 16, 21, 28]
编号为:64的员工从后门入场!拿到双色球的号码是:[03, 14, 20, 23, 24, 26, 27]
编号为:63的员工从后门入场!拿到双色球的号码是:[04, 08, 09, 11, 17, 24, 29]
编号为:62的员工从后门入场!拿到双色球的号码是:[06, 08, 09, 11, 13, 14, 14]
编号为:61的员工从后门入场!拿到双色球的号码是:[01, 04, 07, 20, 24, 28, 31]
编号为:60的员工从后门入场!拿到双色球的号码是:[07, 08, 10, 11, 15, 20, 26]
编号为:59的员工从后门入场!拿到双色球的号码是:[03, 05, 11, 16, 18, 23, 29]
编号为:58的员工从后门入场!拿到双色球的号码是:[04, 07, 08, 12, 19, 26, 31]
编号为:57的员工从后门入场!拿到双色球的号码是:[05, 08, 10, 13, 15, 25, 32]
编号为:56的员工从后门入场!拿到双色球的号码是:[02, 03, 10, 24, 29, 32, 33]
编号为:55的员工从后门入场!拿到双色球的号码是:[02, 09, 10, 12, 18, 28, 29]
编号为:54的员工从后门入场!拿到双色球的号码是:[06, 11, 12, 19, 25, 27, 30]
编号为:53的员工从后门入场!拿到双色球的号码是:[04, 08, 16, 17, 27, 28, 33]
编号为:52的员工从后门入场!拿到双色球的号码是:[08, 12, 16, 18, 29, 30, 32]
编号为:51的员工从后门入场!拿到双色球的号码是:[02, 11, 24, 26, 28, 29, 30]
编号为:50的员工从后门入场!拿到双色球的号码是:[02, 03, 08, 09, 11, 32, 33]
编号为:49的员工从后门入场!拿到双色球的号码是:[02, 06, 10, 11, 13, 28, 33]
编号为:48的员工从后门入场!拿到双色球的号码是:[05, 06, 13, 22, 27, 28, 31]
编号为:47的员工从后门入场!拿到双色球的号码是:[04, 08, 11, 11, 15, 27, 33]
编号为:46的员工从后门入场!拿到双色球的号码是:[07, 10, 12, 14, 23, 25, 28]
编号为:45的员工从后门入场!拿到双色球的号码是:[02, 04, 10, 15, 18, 20, 31]
编号为:44的员工从后门入场!拿到双色球的号码是:[01, 08, 09, 14, 15, 19, 21]
编号为:43的员工从后门入场!拿到双色球的号码是:[05, 17, 25, 27, 29, 30, 31]
编号为:42的员工从后门入场!拿到双色球的号码是:[04, 07, 09, 10, 14, 16, 19]
编号为:41的员工从后门入场!拿到双色球的号码是:[02, 09, 21, 26, 27, 29, 32]
编号为:40的员工从前门入场!拿到双色球的号码是:[01, 03, 05, 12, 18, 23, 32]
编号为:39的员工从后门入场!拿到双色球的号码是:[01, 05, 07, 15, 21, 26, 31]
编号为:38的员工从后门入场!拿到双色球的号码是:[01, 04, 09, 13, 27, 28, 32]
编号为:37的员工从后门入场!拿到双色球的号码是:[05, 10, 16, 18, 21, 23, 29]
编号为:36的员工从后门入场!拿到双色球的号码是:[05, 09, 10, 16, 18, 23, 31]
编号为:35的员工从后门入场!拿到双色球的号码是:[02, 04, 05, 06, 08, 12, 19]
编号为:34的员工从后门入场!拿到双色球的号码是:[02, 15, 16, 17, 20, 24, 29]
编号为:33的员工从后门入场!拿到双色球的号码是:[02, 04, 13, 14, 17, 21, 22]
编号为:32的员工从前门入场!拿到双色球的号码是:[01, 03, 05, 16, 17, 21, 24]
编号为:31的员工从前门入场!拿到双色球的号码是:[03, 05, 09, 18, 22, 26, 29]
编号为:30的员工从前门入场!拿到双色球的号码是:[05, 13, 16, 20, 27, 28, 32]
编号为:29的员工从前门入场!拿到双色球的号码是:[04, 05, 06, 09, 12, 15, 31]
编号为:28的员工从前门入场!拿到双色球的号码是:[08, 12, 14, 20, 22, 24, 26]
编号为:27的员工从前门入场!拿到双色球的号码是:[06, 14, 16, 17, 22, 30, 31]
编号为:26的员工从后门入场!拿到双色球的号码是:[02, 03, 10, 10, 18, 23, 32]
编号为:25的员工从后门入场!拿到双色球的号码是:[04, 11, 14, 17, 20, 21, 30]
编号为:24的员工从后门入场!拿到双色球的号码是:[03, 05, 13, 19, 30, 31, 33]
编号为:23的员工从后门入场!拿到双色球的号码是:[06, 06, 11, 15, 18, 30, 33]
编号为:22的员工从前门入场!拿到双色球的号码是:[02, 02, 08, 16, 19, 23, 28]
编号为:21的员工从前门入场!拿到双色球的号码是:[01, 08, 13, 15, 16, 17, 19]
编号为:20的员工从前门入场!拿到双色球的号码是:[05, 06, 09, 21, 25, 28, 33]
编号为:19的员工从前门入场!拿到双色球的号码是:[06, 06, 25, 26, 27, 32, 33]
编号为:18的员工从前门入场!拿到双色球的号码是:[02, 04, 15, 23, 30, 31, 33]
编号为:17的员工从前门入场!拿到双色球的号码是:[02, 07, 12, 13, 16, 28, 33]
编号为:16的员工从前门入场!拿到双色球的号码是:[01, 02, 12, 14, 16, 22, 23]
编号为:15的员工从前门入场!拿到双色球的号码是:[02, 04, 06, 10, 11, 15, 27]
编号为:14的员工从前门入场!拿到双色球的号码是:[01, 07, 10, 22, 28, 32, 33]
编号为:13的员工从前门入场!拿到双色球的号码是:[01, 01, 05, 10, 13, 27, 32]
编号为:12的员工从前门入场!拿到双色球的号码是:[01, 03, 04, 13, 16, 23, 28]
编号为:11的员工从前门入场!拿到双色球的号码是:[03, 09, 14, 22, 29, 31, 32]
编号为:10的员工从前门入场!拿到双色球的号码是:[03, 06, 07, 08, 13, 21, 29]
编号为:9的员工从前门入场!拿到双色球的号码是:[07, 09, 15, 16, 16, 22, 25]
编号为:8的员工从前门入场!拿到双色球的号码是:[03, 07, 07, 17, 23, 30, 33]
编号为:7的员工从后门入场!拿到双色球的号码是:[01, 09, 10, 14, 17, 20, 25]
编号为:6的员工从后门入场!拿到双色球的号码是:[04, 13, 19, 24, 26, 29, 32]
编号为:5的员工从后门入场!拿到双色球的号码是:[05, 06, 13, 21, 22, 24, 27]
编号为:4的员工从后门入场!拿到双色球的号码是:[04, 08, 09, 14, 16, 17, 22]
编号为:3的员工从后门入场!拿到双色球的号码是:[07, 13, 14, 15, 23, 27, 33]
编号为:2的员工从后门入场!拿到双色球的号码是:[02, 09, 10, 18, 20, 25, 32]
编号为:1的员工从后门入场!拿到双色球的号码是:[04, 08, 13, 22, 23, 26, 28]
从前门入场的员工共有:32位员工
从后门入场的员工共有:68位员工

标签:双色球,号码,编号,员工,前后门,次数,入场,拿到
来源: https://www.cnblogs.com/liqiliang1437/p/13112060.html