Java多線程實現模擬12306火車站售票系統
該系統一共涉及到3個類:
- 車票(Ticket)
- 12306系統(System12306)
- 售票窗口(Window)
車票類,涉及三個屬性:
- 起始站
- 終點站
- 票價
public class Ticket { //起始站 private String startStation; //終點站 private String destination; /**POJO對象,它裡面的基本類型最好使用包裝類 * 因為如果如果該類用來裝從數據庫中讀取的信息 * 如果讀取出null,那麼基本類型是無法存null的,就會報錯*/ //票價 private Float price; public Ticket(){} public Ticket(String startStation, String destination, Float price) { this.startStation = startStation; this.destination = destination; this.price = price; } //為瞭讓打印輸出時方便,我們重寫toString方法 public String toString() { StringBuilder builder = new StringBuilder("["); builder.append(this.startStation); builder.append("--->"); builder.append(this.destination); builder.append(":"); builder.append(this.price); builder.append("]"); return builder.toString(); } //get() and set().... }
12306系統,有一個屬性—>Vector集合,有一個方法—>獲取一張車票getTicket()
- 利用Vector集合存儲該系統所有的車票
- 利用Vector的線程安全特性,同一時間隻能有一個線程操作該集合,保證安全
- 並且該系統應該是單例的
public class System12306 { //設計一個單例模式 private System12306(){} private static volatile System12306 sys; public static System12306 getInstance(){ if (sys == null){ synchronized (System12306.class){ if(sys == null) sys = new System12306(); } } return sys; } //屬性,用一個集合存票,我們使用線程安全的Vector,或者使用Stack,它繼承瞭Vector private Vector<Ticket> tickets = new Vector<>(); private String[] starts = {"北京","天津","太原","運城","西安"}; private String[] ends = {"上海","武漢","蘇州","深圳","昆明","廣州"}; //當前系統創建後,就給tickets集合賦值 { for(int i = 10;i<100;i++){//一共90張票 int r1 = (int)(Math.random()*starts.length); int r2 = (int)(Math.random()*ends.length); float price = r1*r2*5+140; tickets.add(new Ticket(starts[r1],ends[r2],price)); } } //設計一個方法,從集合中獲取一張票 public Ticket getTicket(){ try { return tickets.remove(0); }catch (Exception e){ return null; } } }
售票窗口類,有一個屬性—>窗口名稱,有一個方法—>出售車票sellTicket()
同一時間有多個窗口同時售票,該類實現瞭Runnable接口,達到多線程並發的效果
public class Window implements Runnable { private String windowName;//窗口的名字 public Window(String windowName){ this.windowName = windowName; } @Override public void run() { this.sellTicket(); } public void sellTicket(){ while (true){ System12306 sys = System12306.getInstance(); Ticket ticket = sys.getTicket(); if(ticket==null){ System.out.println("對不起,"+windowName+"窗口車票已售完..."); break; } System.out.println("從"+windowName+"窗口售出:"+ticket); } } }
測試代碼:
public class TestMain { public static void main(String[] args) { Window win1 = new Window("北京西站"); Window win2 = new Window("運城北站"); Window win3 = new Window("太原南站"); new Thread(win1).start(); new Thread(win2).start(); new Thread(win3).start(); } }
多線程並發執行的效果:
到此這篇關於Java多線程實現模擬12306火車站售票系統的文章就介紹到這瞭,更多相關Java 模擬12306售票系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!