java基础之多线程的练习题

13 篇文章 0 订阅

题目如下:

某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票,假设公司有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())+"人");
		
		

	}

}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

deywós

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

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

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

打赏作者

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

抵扣说明:

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

余额充值