1. 程式人生 > >Lesson_for_java_day21--java的多執行緒練習(ATM存取款機、多視窗賣票系統、5人賽跑)

Lesson_for_java_day21--java的多執行緒練習(ATM存取款機、多視窗賣票系統、5人賽跑)

一、ATM存取款機:

package sonyi;

//取款機存取錢練習:

public class TestATM {
	public static void main(String[] args) {
		//開戶
		Account1 a = new Account1(11111, 100);
		System.out.println(a);
		//存取錢
		new Thread(new ATMWindow(a, 1, 50),"ATM1").start();
		new Thread(new ATMWindow(a, 2, 100),"ATM2").start();
		new Thread(new ATMWindow(a, 2, 150),"ATM3").start();
		new Thread(new ATMWindow(a, 1, 50),"ATM4").start();
		new Thread(new ATMWindow(a, 1, 150),"ATM5").start();
		new Thread(new ATMWindow(a, 1, 250),"ATM6").start();
		new Thread(new ATMWindow(a, 2, 250),"ATM7").start();
		new Thread(new ATMWindow(a, 2, 100),"ATM8").start();
		new Thread(new ATMWindow(a, 1, 200),"ATM9").start();	
	}
}

//取款機
class ATMWindow implements Runnable{
	private Account1 ac;
	private int oper;//oper=1 存錢; oper=2 取錢
	private int m; //取或存的金額
	
	public ATMWindow(Account1 ac, int oper, int m) {
		this.ac = ac;
		this.oper = oper;
		this.m = m;
	}
	
	public void run() {
		switch(oper) {
			case 1:
				ac.saveMoney(m);
				break;
			case 2:
				ac.drawMoney(m);
				break;
			default:
				System.out.println("沒有選擇!");
		}
	}
}

//賬戶類
class Account1 {
	private int id;
	private int money;
	
	Account1(int id, int money) {
		this.id = id;
		this.money = money;
	}
	
	//存錢
	public synchronized void saveMoney(int m) {
		money += m;
		System.out.println("賬戶:" + id + "從" + Thread.currentThread().getName() + "中存入" + m + "元,當前餘額為" + money);
		this.notifyAll();//當賬戶中存入錢時,喚醒所有執行緒
	}
	
	//取錢
	public synchronized void drawMoney(int m) {
		while (money < m) {//設定迴圈,當餘額不足時,執行緒等待。
			System.out.println("當前餘額不足!");
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}	
		}
		money -= m;
		System.out.println("賬戶:" + id + "從" + Thread.currentThread().getName() +"中取出" + m + "元,當前餘額為" + money);
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}

	@Override
	public String toString() {
		return "當前賬戶:" + id + ", 餘額為:" + money ;
	}	
}

二、多視窗賣票系統:

package cn.imcore.test;
//多視窗賣票系統
public class TestTicket {
	public static void main(String[] args) {		
		Ticket2 ticket = new Ticket2();
		new Thread(ticket, "視窗1").start();
		new Thread(ticket, "視窗2").start();
		new Thread(ticket, "視窗3").start();
	}
}

class Ticket2 implements Runnable{
	int ticket = 100;
	public void run() {
		while(true) {
			synchronized (this) {
				if(ticket==0) 
					break;
				System.out.println(Thread.currentThread().getName() + ":賣出第" + (ticket--) + "張票");
			}			
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

三、5人賽跑比賽:

package sonyi;
//5位選手同時賽跑,記錄最先到達終點的人,並結束比賽。列印冠軍選手。

public class WinnerTest {
	public static void main(String[] args) {
		Thread t1 = new Man("選手1");
		Thread t2 = new Man("選手2");
		Thread t3 = new Man("選手3");
		Thread t4 = new Man("選手4");
		Thread t5 = new Man("選手5");
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		
		try {
			t1.join();
			t2.join();
			t3.join();
			t4.join();
			t5.join();
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Man.winName + "跑贏了");
	}
}

//比賽人員執行緒
class Man extends Thread {
	static boolean flag = false;//比賽未結束
	public static String winName = "";
	private String name;
	
	public Man(String name) {
		this.name = name;
	}
	
	public void run() {
		int len = 1;
		while(!flag) {
			if(flag) break;
			if(len==10){
				flag = true;//比賽結束
				winName = name;
				break;
			}
			System.out.println(name + "跑了" + len + "米。");
			len++;
		}
	}
}