1. 程式人生 > >java基礎之多執行緒的練習題

java基礎之多執行緒的練習題

題目如下:

某公司組織年會,會議入場時有兩個入口,在入場時每位員工都能獲取一張雙色球彩票,假設公司有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())+"人");
		
		

	}

}