1、class:Main2
package cn.com.chysoft.demo2;
/**
* 多線程模擬購票系統
*
* @author chenyong QQ:369232566
* @date 2013-03-21 下午14:10
*/
public class main2 {
/**
* 多線程共享
*
*@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stb
TicketCentre centre = new TicketCen tre(10);// 初始化票務中心的票數
Instance instance1 = new Instance(200000, new Ticket(1, "張三", centre));
Instance instance2 = new Instance(800000, new Ticket(2, "王八", centre));
Instance instance3 = new Instance(400000, new Ticket(3, "狗子", centre));
Instance instance4 = new Instance(500000, new Ticket(1, "學妹", centre));
Instance instance5 = new Instance(300000, new Ticket(3, "李五哥", centre));
Instance instance6 = new Instance(100000, new Ticket(1, "習哥", centre));
Thread t1 = new Thread(instance1, "南站(1號窗口)");
Thread t2 = new Thread(instance2, "客運中心(2A號窗口)");
Thread t3 = new Thread(instance3, "南站(2號窗口)");
Thread t4 = new Thread(instance4, "南站(2號窗口)");
Thread t5 = new Thread(instance5, "南站(1號窗口)");
Thread t6 = new Thread(instance6, "客運中心(1B號窗口)");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
}
}
//====================================================================================================================
2、多線程實現類 Instance
package cn.com.chysoft.demo2;
import java.text.SimpleDateFormat;
/**
* 線程實現
*
* @author chenyong QQ:369232566
* @date 2013-03-21 下午14:14
*/
public class Instance implements Runnable {
private Ticket ticket;
private boolean isOk;// 是否購票成功
private TicketCentre centre;// 票務中心
private int wtime;// 購票耗時
public Instance(int wtime, Ticket ticket) {
this.wtime = wtime;
this.ticket = ticket;
}
private final void fun(int wtime) {
this.centre = ticket.getCentre();
try {
this.isOk = this.centre.buy(this.ticket);// 購買
/**
* 設置餘票,因爲對象中的數值是共享的
*/
this.showMessage(this.ticket.getCentre().getSurplus());
Thread.sleep(wtime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void showMessage(int current) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSSSSSS --> ");
StringBuffer sbCtx = new StringBuffer(sdf.format(this.ticket.getTime().getTime()));
sbCtx.append(this.ticket.getUser());
if (this.isOk) {
sbCtx.append(" 在 ").append(Thread.currentThread().getName()).append(" 購買 ").append(this.ticket.getNum()).append(" 張票, 當前剩餘 ").append(current).append(" 張。");
} else {
sbCtx.append(" 在 ").append(Thread.currentThread().getName()).append(" 購買失敗!").append("剩餘 ").append(current).append(" 張。");
}
System.out.println(sbCtx);
}
public void run() {
this.fun(this.wtime);
}
}
//====================================================================================================================
3、車票基本信息
package cn.com.chysoft.demo2; import java.util.Date; /** * 購票信息 * * @author chenyong QQ:369232566 * @date 2013-03-21 下午14:14 */ public class Ticket { private String user;// 購買人 private int num;// 購票數量 private Date time;// 購票時間 private TicketCentre centre;// 購票中心 /** * 初始化車票信息 * * @param num * @param user * @param centre */ public Ticket(int num, String user, TicketCentre centre) { this.num = num; this.user = user; this.centre = centre; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public void setTime(Date time) { this.time = time; } public Date getTime() { return time; } public TicketCentre getCentre() { return centre; } public void setCentre(TicketCentre centre) { this.centre = centre; } }
//====================================================================================================================
4、票務中心處理
package cn.com.chysoft.demo2; import java.util.Date; /** * 購票中心 * * @author chenyong QQ:369232566 * @date 2013-03-21 下午14:01 */ public class TicketCentre { private int tatol = 0;// 車站票數 private int current = 0;// 當前剩餘票數 private boolean flag = false;// 是否購完 /** * 初始化總票值 * * @param tatol */ public TicketCentre(int tatol) { this.tatol = this.current = tatol; } /** * 購票 * * @param ticket * @return */ public synchronized boolean buy(Ticket ticket) { ticket.setTime(new Date()); if (!this.getFlag()) {// 還有餘票 this.current = this.current - ticket.getNum(); if (this.current >= 0) { if (this.current == 0) {// 當前已無票 this.flag = true; } return true; } else { // 購買票數多於餘票 this.current = this.current + ticket.getNum(); } } return false; } /** * 餘票查詢 * * @return */ public int getSurplus() { return this.current; } public int getTatol() { return tatol; } public int getCurrent() { return current; } public boolean getFlag() { return flag; } }
輸出結果如下,因線程調度順序和結果不一: