java多线程模拟实现12306售票

业务描述
假设有200张票,用4个线程去订票,不能有两个或者以上的线程订到了
同一个票(原因就不说了),当最后一张票卖掉的时候结束,再订就抛
异常提示出票卖完了。

业务分析,要实现以上功能,

1、需要创建一个车票类,初始化票,卖票的接口saleTicket()

2、自定义异常的一个类。

3、创建卖票线程类,在run方法中卖车票。

4、初始化车票的线程,负责初始化车票,也就是初始化Ticket类中的数组。

5、创建主方法进行测试。

 

扔上我的代码

 

public class Ticket {
    //票务系统初始化票务信息可以容纳的容量
    private String[] str = new String[1000];

    public String[] getStr() {
        return str;
    }

    public void setStr(String[] str) {
        this.str = str;
    }

    //指针指向第一张票
    private int index = 0;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    //初始化票务信息
    public void initializeTicket(){
        for (int i = 0; i < 200; i++) {
            str[i] = "车票编号:"+(i+1);
        }
    }
}
//卖票接口
interface SaleTicket {
    String saleTicket() throws NullTicketException;
}
public class InitializeTicket extends Thread {
    private Ticket ticket;

    public InitializeTicket(Ticket ticket) {
        this.ticket = ticket;
    }

    @Override
    public void run() {
        ticket.initializeTicket();
    }
}
public class NullTicketException extends Exception {
    public NullTicketException(){}
    public NullTicketException(String msg){
        super(msg);
    }
}
public class TicketThread extends Thread implements SaleTicket{
     private Ticket tic;
     public TicketThread(Ticket tic) {
         this.tic = tic;
     }
     @Override
     public void run() {
         for (int i = 0; i < 60; i++) {
             try {
                 String s = this.saleTicket();
                 System.out.println(this.getName()+"成功购票,购得车票--->"+s);
             } catch (NullTicketException e) {
                 System.out.println(e);
             }
         }
     }

     @Override
     public String saleTicket() throws NullTicketException {
         synchronized (tic){
             if(null != tic.getStr()[tic.getIndex()]){
                 String s =tic.getStr()[tic.getIndex()];
                 //给ticket指针向后移位
                 tic.setIndex(1+tic.getIndex());
                 return s;
             }else {
                 throw new NullTicketException("车票已售罄");
             }
         }
     }
 }
public class Test {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread formatTicket = new InitializeTicket(ticket);//初始化票务信息
        formatTicket.run();
        try {
            formatTicket.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //新建四个卖票线程
        Thread t1 = new TicketThread(ticket);
        Thread t2 = new TicketThread(ticket);
        Thread t3 = new TicketThread(ticket);
        Thread t4 = new TicketThread(ticket);
        t1.setName("1号售票口");
        t2.setName("2号售票口");
        t3.setName("3号售票口");
        t4.setName("4号售票口");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

 

测试结果如下图

java多线程模拟实现12306售票_第1张图片

java多线程模拟实现12306售票_第2张图片

你可能感兴趣的:(java多线程模拟实现12306售票)