Problem A: 实验十二:窗口卖票

Problem Description

1.实验目的
(1) 掌握线程的创建、使用及sleep方法的运用

2.实验内容
  编写一个售票系统,三个窗口,每个窗口独立售票,所售票不能有重号。并且优先次序为1号窗口卖1-5号,2号窗口卖6-10号,3号窗口卖11-15号。

3.实验要求
  请完成下列代码:
   class MyThreadR implements Runnable{
 
     // 你的代码(注意,上面的代码属于MyThreadR类,下面的代码属于Main类)
     
       th1.start();
       th1.sleep(3000);
       th2.start();
       th2.sleep(3000);
       th3.start();
     }
  }

Output Description

Win1 sales 1 ticket
Win1 sales 2 ticket
Win1 sales 3 ticket
Win1 sales 4 ticket
Win1 sales 5 ticket
Win2 sales 6 ticket
Win2 sales 7 ticket
Win2 sales 8 ticket
Win2 sales 9 ticket
Win2 sales 10 ticket
Win3 sales 11 ticket
Win3 sales 12 ticket
Win3 sales 13 ticket
Win3 sales 14 ticket
Win3 sales 15 ticket
 
ac代码:
 1  static int num = 0;
 2     int ticketnum = 15;
 3     public void run(){
 4         for (int i = 0; i < 5; i++) {
 5             num++;
 6             if( num<=ticketnum ) {
 7                 System.out.println(Thread.currentThread().getName() + " sales " + num + " ticket");
 8             }
 9         }
10     }
11 }
12 class Main{
13     public static void main (String[] args)throws InterruptedException {
14         MyThreadR th = new MyThreadR();
15         Thread th1 = new Thread(th,"Win1");
16         Thread th2 = new Thread(th,"Win2");
17         Thread th3 = new Thread(th,"Win3");

 

posted @ 2023-05-29 16:47  hangsingplus  Views(32)  Comments(0Edit  收藏  举报