zoukankan      html  css  js  c++  java
  • 第九周课程总结&实验报告

    实验任务详情:

    完成火车站售票程序的模拟。
    要求:
    (1)总票数1000张;
    (2)10个窗口同时开始卖票;
    (3)卖票过程延时1秒钟;
    (4)不能出现一票多卖或卖出负数号票的情况。

    实验源码

    class test implements Runnable {
    static int ticket = 999;
    public void run() {
    for (int i = 0; i < 1000; i++)
    synchronized (this) {
    if (ticket >= 0) {
    if (ticket == 0) {
    System.out.println(Thread.currentThread().getName() + "票已售罄");
    break;}
    else {
    try {
    Thread.sleep(1000);
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + "卖出一张票" + " " + "所剩余票为:" + ticket);
    ticket--;
    }
    }
    }
    }
    }

    public class cao {
    public static void main(String args[]) {
    test t = new test();
    Thread th1 = new Thread(t, "窗口1");
    Thread th2 = new Thread(t, "窗口2");
    Thread th3 = new Thread(t, "窗口3");
    Thread th4 = new Thread(t, "窗口4");
    Thread th5 = new Thread(t, "窗口5");
    Thread th6 = new Thread(t, "窗口6");
    Thread th7 = new Thread(t, "窗口7");
    Thread th8 = new Thread(t, "窗口8");
    Thread th9 = new Thread(t, "窗口9");
    Thread th10 = new Thread(t, "窗口10");

    th1.start();
    th2.start();
    th3.start();
    th4.start();
    th5.start();
    th6.start();
    th7.start();
    th8.start();
    th9.start();
    th10.start();
    }
    }

    实验结果

  • 相关阅读:
    httpclient详细介绍
    HttpClient
    JAVA WEB项目中各种路径的获取
    mvn 如何添加本地jar包 IDEA pom.xm
    jdbc连接数据库的步骤
    PostgreSQL 类型转换 -除法
    PostgreSQL学习手册
    前端工程师必备技能汇总
    github上一些觉得对自己工作有用的项目收集
    Ninja:Java全栈Web开发框架-Ninja中文网
  • 原文地址:https://www.cnblogs.com/qq2676311181/p/11739469.html
Copyright © 2011-2022 走看看