码迷,mamicode.com
首页 > 其他好文 > 详细

第九周课程总结&实验报告(七)

时间:2019-10-22 23:57:19      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:源代码   set   station   runnable   pack   nstat   info   new   string   

实验内容:
源代码:

主类:




package exercise;
public class TrainStationTicketSales implements Runnable {
    private int ticket = 1000;
    public void run() {
        for(int i = 0 ; i<1000;i++) {
            this.sale();
            ticket--;
        }
    }
public synchronized void sale() {
        if(ticket>=0){
            try {
                Thread.sleep(100);
            
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"卖出一张票"+" "+"余票:" +ticket);
        }
        if(ticket==0) {
            System.out.println("票已销完");
            
        }
    }
}

测试类:




package exercise;

public class TrainStationTest {
    public static void main(String args[]) {
        TrainStationTicketSales ts = new TrainStationTicketSales();
//      TrainStationTicketSales ts1 = new TrainStationTicketSales();
        Thread t2 = new Thread(ts,"二号窗口");
        Thread t1 = new Thread(ts,"一号窗口");
        Thread t3 = new Thread(ts,"三号窗口");
        Thread t4 = new Thread(ts,"四号窗口");
        Thread t5 = new Thread(ts,"五号窗口");
        Thread t6 = new Thread(ts,"六号窗口");
        Thread t7 = new Thread(ts,"七号窗口");
        Thread t8 = new Thread(ts,"八号窗口");
        Thread t9 = new Thread(ts,"九号窗口");
        Thread t10 = new Thread(ts,"十号窗口");
        t10.setPriority(Thread.MAX_PRIORITY);
        t1.setPriority(Thread.MAX_PRIORITY);
        t9.setPriority(Thread.MAX_PRIORITY);
        t6.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        t4.setPriority(Thread.MIN_PRIORITY);
        t5.setPriority(Thread.MIN_PRIORITY);
        t7.setPriority(Thread.MIN_PRIORITY);
        t3.setPriority(Thread.NORM_PRIORITY);
        t8.setPriority(Thread.NORM_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t7.start();
        t8.start();
        t9.start();
        t10.start();
    }
}

运行截图:

技术图片
技术图片
技术图片
技术图片
技术图片

思路

第九周课程总结&实验报告(七)

标签:源代码   set   station   runnable   pack   nstat   info   new   string   

原文地址:https://www.cnblogs.com/xudo/p/11723427.html

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