亲宝软件园·资讯

展开

Java 模拟12306售票系统 Java多线程实现模拟12306火车站售票系统

发光吖 人气:0
想了解Java多线程实现模拟12306火车站售票系统的相关内容吗,发光吖在本文为您仔细讲解Java 模拟12306售票系统的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,模拟12306售票,Java,12306售票,下面大家一起来学习吧。

该系统一共涉及到3个类:

车票类,涉及三个属性:

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()

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();
    }
}

多线程并发执行的效果:

在这里插入图片描述

加载全部内容

相关教程
猜你喜欢
用户评论