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; } }
输出结果如下,因线程调度顺序和结果不一: