【无标题】

该博客展示了如何使用Java实现一个多线程售票系统。`Ticket`类包含了票号、车次、始发地和目的地等信息。`Ticketer`类继承自`Thread`,用于模拟售票员角色,从票池中售卖并移除票。在测试类`Test`中,创建了两个售票员线程,共享同一票池,并开始售票。此示例演示了线程同步的基本概念。
摘要由CSDN通过智能技术生成

火车卖票
Ticket类(车票)
    private Integer ticketNum; //票号
    private String trainNum; //车次
    private String Origin; //始发地
    private String destination; //目的地

创建ticket

package cn.hp.train;

public class Ticket {
    private Integer ticketNum;//票号
    private String trainNum;//车次
    private String Origin;//始发地
    private String destination;//目的地


    @Override
    public String toString() {
        return "票号:" + ticketNum +
                ", 车次:'" + trainNum + '\'' +
                ", 始发地:'" + Origin + '\'' +
                ", 目的地:'" + destination + '\'' ;
    }

    public Ticket(Integer ticketNum, String trainNum, String origin, String destination) {
        this.ticketNum = ticketNum;
        this.trainNum = trainNum;
        Origin = origin;
        this.destination = destination;
    }

    public Ticket() {
    }

    public Integer getTicketNum() {
        return ticketNum;
    }

    public void setTicketNum(Integer ticketNum) {
        this.ticketNum = ticketNum;
    }

    public String getTrainNum() {
        return trainNum;
    }

    public void setTrainNum(String trainNum) {
        this.trainNum = trainNum;
    }

    public String getOrigin() {
        return Origin;
    }

    public void setOrigin(String origin) {
        Origin = origin;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

}

创建ticketer

package cn.hp.train;

import java.util.List;

public class Ticketer extends  Thread{
    private List<Ticket> tickets;//票房(多个售票员同一个售票)

    //卖票方法(卖一张从集合就要删一张票)
    public void sellingTickets(){
        Ticket ticket = this.tickets.remove(0);
        System.out.println(this.getName()+"买了一张"+ticket);
    }

    @Override
    public void run() {
        while (true) {
            //同一cpu,只有一个线程的锁,只能拿到一个
            synchronized (this.tickets) {
                if (this.tickets.size() > 0) {
                    sellingTickets();
                } else {
                    System.out.println(this.getName() + "售完");
                    break;
                }
            }
        }
    }

    @Override
    public String toString() {
        return "Ticketer{" +
                "tickets=" + tickets +
                '}';
    }

    public List<Ticket> getTickets() {
        return tickets;
    }

    public void setTickets(List<Ticket> tickets) {
        this.tickets = tickets;
    }
}

测试类Test

package cn.hp.train;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        //票房

        List<Ticket> tickets = new ArrayList<>();
        for (int i = 1; i <=20; i++) {
            Ticket ticket = new Ticket(i,"G2022","郑州","重庆");
            tickets.add(ticket);
        }
        Ticketer t1 = new Ticketer();
        t1.setName("小红");
        Ticketer t2 = new Ticketer();
        t2.setName("小丽");
        //给两个售票员设置同一个票房
        t1.setTickets(tickets);
        t2.setTickets(tickets);
        //开始子线程进行售票
        t1.start();
        t2.start();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值