多窗口售票
多线程–多窗口售票,每个窗口卖掉一张票时间不同
代码实现方式一:继承Thread类
import java.util.ArrayList;
import java.util.Collections;
public class Test03 extends Thread {
// 通过构造方法给线程名字赋值
public Test03(String name) {
super(name); // 给线程名字赋值
}
static ArrayList<String> list=getList(); //座位号集合
// 票数为座位号集合长度-1
static int tick = list.size()-1;
static Object ob=new Object();
public static ArrayList<String> getList(){ //获取打乱的票座位号集合方法
ArrayList<String> list = new ArrayList<>();
String[] str={"A","B","C","D","E","F","G","H","I","J"};
int[] num={1,2,3,4,5,6,7,8,9,10};
for (int i : num) {
for (String s : str) {
list.add(s+i); //组合
}
}
Collections.shuffle(list); //打乱
return list;
}
public void sleepTime(){ //每个售票窗口买一张票需要时间方法
if (getName().equals("电影院")){
try {
sleep(3000); //休息3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (getName().equals("时光网")){
try {
sleep(5000); //休息5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (getName().equals("美团")){
try {
sleep(2000); //休息2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (getName().equals("支付宝")){
try {
sleep(6000); //休息6秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 重写run方法,实现买票操作
@Override
public void run() {
while (tick >= 0) {
sleepTime();
synchronized (ob) {
if (tick >= 0) {
System. out .println(getName() + "卖出了一张票,座位号为:" + list.get(tick) + ",剩余票数:"+tick );
tick--;
} else {
System. out .println( "票卖完了" );
}
}
}
}
public static void main(String[] args) {
//实例化窗口对象,并为每一个窗口取名字
Test03 t1= new Test03( "电影院" );
Test03 t2= new Test03( "时光网" );
Test03 t3= new Test03( "美团" );
Test03 t4= new Test03( "支付宝" );
// 开启线程
t1.start();
t2.start();
t3.start();
t4.start();
}
}
代码实现方式二:实现Runnable接口
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.Thread.sleep;
public class JslTest03 implements Runnable {
static ArrayList<String> list=getList(); //座位号集合
// 票数为座位号集合长度-1
static int tick = list.size()-1;
public static ArrayList<String> getList(){ //获取打乱的票座位号集合 方法
ArrayList<String> list = new ArrayList<>();
String[] str={"A","B","C","D","E","F","G","H","I","J"};
int[] num={1,2,3,4,5,6,7,8,9,10};
for (int i : num) {
for (String s : str) {
list.add(s+i); //组合
}
}
Collections.shuffle(list); //打乱
return list;
}
public void sleepTime(){ //每个售票窗口买一张票需要时间方法
if (Thread.currentThread().getName().equals("电影院")){
try {
sleep(3000); //休息3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (Thread.currentThread().getName().equals("时光网")){
try {
sleep(5000); //休息5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (Thread.currentThread().getName().equals("美团")){
try {
sleep(2000); //休息2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (Thread.currentThread().getName().equals("支付宝")){
try {
sleep(6000); //休息6秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void syn(){ //同步方法
if (tick >= 0) {
System. out .println(Thread.currentThread().getName() + "卖出了一张票,座位号为:" + list.get(tick) + ",剩余票数:"+tick );
tick--;
} else {
System. out .println( "票卖完了" );
}
}
@Override
public void run() {
while (tick >= 0) {
sleepTime();
syn();
}
}
public static void main(String[] args) {
JslTest03 js = new JslTest03();
//创建线程
Thread t1 = new Thread(js);
Thread t2 = new Thread(js);
Thread t3 = new Thread(js);
Thread t4 = new Thread(js);
// 设置线程名称,并开启线程
t1.setName("电影院");
t1.start();
t2.setName("时光网");
t2.start();
t3.setName("美团");
t3.start();
t4.setName("支付宝");
t4.start();
}
}
运行效果
![49](https://gitee.com/panqiyi/pqimg/raw/master/20201230144206.gif)