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

Posted xudo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第九周课程总结&实验报告相关的知识,希望对你有一定的参考价值。

实验内容:
源代码:

主类:




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();
    }
}

运行截图:

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

思路

以上是关于第九周课程总结&实验报告的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

课程总结

第九周总结&实验报告七

201621123054《Java程序设计》第九周学习总结