多线程-多窗口售票课程设计

多窗口售票

多线程–多窗口售票,每个窗口卖掉一张票时间不同

image-20201230142655141

代码实现方式一:继承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
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值