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

12 篇文章 0 订阅
该文章描述了一个Java编程实验,目的是掌握线程的创建和使用,以及如何运用sleep方法。实验内容是一个有三个窗口的售票系统,每个窗口按顺序销售1-15号票,且票号不重复。实验中使用了ReentrantLock进行同步控制,确保线程安全地依次售票。
摘要由CSDN通过智能技术生成

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

 

 Input 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

 

 Output Description

 

Sample Input

 Sample Output

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

 Hint

 我的想法:

 我的代码:

package com.chenjiu;

import java.util.concurrent.locks.ReentrantLock;

class MyThreadR implements Runnable {

    private static int total = 1;
    private Object obj = new Object();

    @Override
    public void run() {
        while(true){
            synchronized (obj) {
                if (total > 15) {
                    return;
                }
                if ("Win1".equals(Thread.currentThread().getName())) {
                    if (total < 5 && total > 0) {
                        for (int i = total; i < total + 5; i++) {
                            System.out.println(Thread.currentThread().getName() + " sales " + i + " ticket");
                        }
                        total = total + 5;
                        return;
                    }

                }

                if ("Win2".equals(Thread.currentThread().getName())) {
                    if (total < 10 && total >= 5) {
                        for (int i = total; i < total + 5; i++) {
                            System.out.println(Thread.currentThread().getName() + " sales " + i + " ticket");
                        }
                        total = total + 5;
                        return;
                    }
                }
                if ("Win3".equals(Thread.currentThread().getName())) {
                    if (total < 15 && total >= 10) {
                        for (int i = total; i < total + 5; i++) {
                            System.out.println(Thread.currentThread().getName() + " sales " + i + " ticket");
                        }
                        total = total + 5;
                        return;
                    }
                }
            }
        }

    }
}


class Main {
    public static void main(String[] args) throws InterruptedException {

        MyThreadR th = new MyThreadR();
        Thread th1 = new Thread(th, "Win1");
        Thread th2 = new Thread(th, "Win2");
        Thread th3 = new Thread(th, "Win3");
        // 你的代码(注意,上面的代码属于MyThreadR类,下面的代码属于Main类)

        th1.start();
        th1.sleep(3000);
        th2.start();
        th2.sleep(3000);
        th3.start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小木苓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值