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++;
		}
	}
}


你可能感兴趣的:(lessonForJava)