多线程练习(简单模拟火车站多窗口同时售票)

  • 模拟火车站售票窗口同时售票

 

public class xianchenglianxi {
    public static void main(String arg[]){
        long begin = System.currentTimeMillis(); 
        MyThread myth_1 = new MyThread("1");        //创建线程对象
        MyThread myth_2 = new MyThread("2");
        MyThread myth_3 = new MyThread("3");
        MyThread myth_4 = new MyThread("4");
        MyThread myth_5 = new MyThread("5");
        myth_1.setPriority(5);        //设置线程优先级 1最低 10最高
        myth_2.setPriority(6);
        myth_3.setPriority(10);
        myth_4.setPriority(1);
        myth_5.setPriority(8);
        myth_1.start();                //启动线程
        myth_2.start();
        myth_3.start();
        myth_4.start();
        myth_5.start();
    }
}
class MyThread extends Thread {        //继承Thread类,为了调用其run方法
    String name;            
    int ticket = 5;            //票总数    
    public MyThread(String name){
        this.name = name;
    }
    public void run(){                //方法重写
        for(int i = ticket;i >= 0;i--){
            if(i > 0){
                System.out.println("窗口:"+name+"售票成功"+"\t"+"余票"+i);
            }
            else{
                System.out.println("窗口:"+name+"售票成功\t余票 "+i+"\t窗口关闭");
            }
        }
        
    }
}

 

转载于:https://www.cnblogs.com/string9527/p/7157375.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值