功能实现:多线程模拟售票系统
定义一个车票类,车票类拥有票号属性、 车票状态(未售、售出)属性和拥有者(String类型)属性;在主线程中定义一个票库(ArrayList数组列表)初始化票库并生产200张票;
定义一个乘客类继承自线程类,有一个名字(String类型)属性, 乘客类每隔10毫秒就去票库中查看有没票,如果有票就抢一张过来 (把车票标为售出状态,并把车票的拥有者标为自己的名字), 每个乘客抢到20张票就停止运行。
在主线程中创建10个乘客类并启动它们(开始抢票), 要求每个乘客抢到的票不连号,不能多抢或少抢。抢完后打印出每个人抢到的票号。
代码实现部分:
import java.util.ArrayList;
public class Main {
static ArrayList<Ticket> mTicketList = new ArrayList<Ticket>();//票库
public static void main(String[] args) {
for(int i=0;i<200;i++){
Ticket t = new Ticket();
t.ticketNum = i + 100;
mTicketList.add(t);
}
Passenger[] passengers = new Passenger[10];
for(int i=0;i<10;i++){
passengers[i] = new Passenger(“passenger_”+i/*,mTicketList*/);
passengers[i].start();
}
//等待所有乘客结束
for(int i=0;i<10;i++){
try {
passengers[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int i=0;i<passengers.length;i++){
System.out.print(passengers[i].name+”抢到的票:”);
int tCnt = 0; //抢到的票数
for(int j=0;j<mTicketList.size();j++){
Ticket t = mTicketList.get(j);
if(t != null && t.owner != null && passengers[i] != null){
if(t.owner.equals( passengers[i].name)){
tCnt++;
System.out.print(t.ticketNum+”,”);
}
}
}
System.out.println(“总票数:”+tCnt);
}
}
}
import java.util.ArrayList;
public class Passenger extends Thread {
String name;//名字
//ArrayList<Ticket> mTicketList ;
int myTickets = 0;
public Passenger(String name/*,ArrayList<Ticket> ticketList*/){
//mTicketList = ticketList;
this.name = name;
}
@Override
public void run() {
//Main.mTicketList.get(0);
while(myTickets<20){
//开始抢票
for(int i=0;i<Main.mTicketList.size();i++){
Ticket t = Main.mTicketList.get(i); //取出第i张票
synchronized (t) { //synchronized (监视的竞争对象)
if(t.saledState == Ticket.STATE_NOT_SALED){
t.saledState = Ticket.STATE_SALED; //把车票标为售出状态
t.owner = this.name; //把车票的拥有者标为自己的名字
myTickets++;
System.out.println(this.name +”抢到”+t.ticketNum+”号票”);
break;
}
}
/*
boolean result = checkTicket(t);
if(result){
break;
}*/
}
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/*
* synchronized 默认监视的竞争对象 : 为当前类对象,即this
*/
public synchronized boolean checkTicket(Ticket t){
if(t.saledState == Ticket.STATE_NOT_SALED){
t.saledState = Ticket.STATE_SALED; //把车票标为售出状态
t.owner = this.name; //把车票的拥有者标为自己的名字
myTickets++;
System.out.println(this.name +”抢到”+t.ticketNum+”号票”);
return true;
}
return false;
}
}
public class Ticket {
public static final boolean STATE_SALED = true; //售出状态
public static final boolean STATE_NOT_SALED = false;//未售出状态
public int ticketNum = 0; //票号
public boolean saledState = STATE_NOT_SALED;//车票状态
public String owner = null; //拥有者
}
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14028.html