题目如下:
某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票,假设公司有100个员工,利用多线程模拟年会入场过程,
并分别统计每个入口入场的人数,以及每个员工拿到的彩票的号码。线程运行后打印格式如下:
编号为: 2 的员工 从后门 入场! 拿到的双色球彩票号码是: [17, 24, 29, 30, 31, 32, 07]
编号为: 1 的员工 从前门 入场! 拿到的双色球彩票号码是: [06, 11, 14, 22, 29, 32, 15]
//.....
从后门入场的员工总共: 13 位员工
从前门入场的员工总共: 87 位员工
代码如下:
员工类,继承与Runnable接口,并且实现run方法
public class employer implements Runnable {
private static int count = 100;
private static int back=0;
private static int front=0;
public static int getCount() {
return count;
}
public static void setCount(int count) {
employer.count = count;
}
public static int getBack() {
return back;
}
public static void setBack(int back) {
employer.back = back;
}
public static int getFront() {
return front;
}
public static void setFront(int front) {
employer.front = front;
}
@Override
public void run() {
while (count > 1) {
synchronized (this) {
if("前门".equals(Thread.currentThread().getName())){
front++;
}else if("后门".equals(Thread.currentThread().getName())){
back++;
}
System.out.println("第"+count--+"名员工,从"+//
Thread.currentThread().getName()+"出,取出的双色球的号码为:"+Lottery.getResult());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
双色球生成类,生成红蓝两种颜色的球。
import java.util.Arrays;
import java.util.Random;
/**
*
* 彩票生产类
*/
public class Lottery {
private static String[] redpool = {"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"};
private static String[] bluepool = {"01", "02", "03", "04", "05", "06", "07",//
"08", "09", "10", "11", "12", "13", "14", "15", "16"};
public static String [] resultball=null;
public Lottery() {
// 红球 1-33取出6个
// 兰球1-16 取一个
// 随机生成一组双色球号码:
// String[] redpool ={"01","02","03"};
//getball(redpool, bluepool);
}
public static String[] getball(String[] red, String[] blue) {
Random x = new Random();
resultball=new String[7];
String[] strred = new String[6];// 存储红色
String strblue = "";// 存储蓝色
int index = 0;// 游标
for (int i = 0; i < strred.length; i++) {
index = x.nextInt(33);//set random seeds
strred[i] = red[index];//
for (int j = 0; j < i; j++) {
if (strred[j] == red[index]) {
//System.out.println("double:" + red[index] + " delete");
i--;
break;
}
}
}
int index2 = x.nextInt(16);
strblue = blue[index2];
//System.out.print("红色球为" + Arrays.toString(strred));
//System.out.println("蓝色球为" + strblue);
//resultball=strred;
System.arraycopy(strred,0,resultball,0,6);
resultball[resultball.length-1]=strblue;
//System.out.println("双色球抽奖结果为:"+Arrays.toString(resultball));
return resultball;
}
public static String getResult(){
return Arrays.toString(getball(redpool, bluepool));
}
public static void main(String[] args) {
//System.out.println(Arrays.toString(getball(redpool, bluepool)));
}
}
测试类:
public class XMain {
public static void main(String[] args) {
employer my = new employer();
Thread t1 = new Thread(my, "前门");
Thread t2 = new Thread(my, "后门");
t1.start();// 同一个mt,但是在Thread中就不可以,如果用同一个实例化对象mt,就会出现异常
// new Thread(my, "E").start();
t2.start();
if (t1.isAlive()||t2.isAlive()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("前门总计:"+employer.getFront()+"人\t后门总计:"+employer.getBack()+"人\t总计:"+(100-employer.getCount())+"人");
}
}