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

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

     实验代码

    package demo3;
    
    public class MyThread implements Runnable{
                private int ticket=1000;
                
                public void run() {
                    for(int i=0;i<1001;i++) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        synchronized(this) {

                    if(ticket>0) {
                      System.out.println(Thread.currentThread().getName()+"卖票:ticket="+ticket--);

                  }
            }
          }
        }
    }

    package demo3;
    
    public class Test {
    
        public static void main(String[] args) {
            MyThread mt = new MyThread();
    
            Thread t1=new Thread(mt,"窗口1");
            Thread t2=new Thread(mt,"窗口2");
            Thread t3=new Thread(mt,"窗口3");
            Thread t4=new Thread(mt,"窗口4");
            Thread t5=new Thread(mt,"窗口5");
            Thread t6=new Thread(mt,"窗口6");
            Thread t7=new Thread(mt,"窗口7");
            Thread t8=new Thread(mt,"窗口8");
            Thread t9=new Thread(mt,"窗口9");
            Thread t10=new Thread(mt,"窗口10");
            
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
            t6.start();
            t7.start();
            t8.start();
            t9.start();
            t10.start();
        }
    
    }
     

    实验结果

     

     学习总结

    主要学习了多线程的应用,其中有实现raunnable类和继承Thread类;还有一些线程操作的主要方法;如:线程的休眠、强制运行、礼让等;还简单的了解了java的IO。

  • 相关阅读:
    WebApi Owin SelfHost OAuth2
    HTML5 localStorage、sessionStorage 作用域
    Owin WebApi版本控制
    C# Guid 16位 唯一
    C# TimeSpan获取 年月
    ASP.NET Web Api OwinSelfHost Restful 使用
    UrlRouteModule
    asp.net 代码 注意点
    JS小问题总结
    JS中javascript:void(0)真正含义
  • 原文地址:https://www.cnblogs.com/liuz98/p/11736619.html
Copyright © 2011-2022 走看看