码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-12 21:31:21      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:art   public   log   this   xtend   优先级   new   strong   soft   

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

 

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窗口关闭");
            }
        }
        
    }
}

 

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

标签:art   public   log   this   xtend   优先级   new   strong   soft   

原文地址:http://www.cnblogs.com/string9527/p/7157375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!