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

    � 提交作业

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

    package 测试;
    
    public class 火车站系统 {
    	public static void main(String[] agrs) {
    		MyThread mt =new MyThread();
    		Thread t1 =new Thread(mt,"一号窗口");
    		Thread t2 =new Thread(mt,"二号窗口");
    		Thread t3 =new Thread(mt,"三号窗口");
    		Thread t4 =new Thread(mt,"四号窗口");
    		Thread t5 =new Thread(mt,"五号窗口");
    		Thread t6 =new Thread(mt,"六号窗口");
    		Thread t7 =new Thread(mt,"七号窗口");
    		Thread t8 =new Thread(mt,"八号窗口");
    		Thread t9 =new Thread(mt,"九号窗口");
    		Thread t10 =new Thread(mt,"十号窗口");
    		t1.start();
    		t2.start();
    		t3.start();
    		t4.start();
    		t5.start();
    		t6.start();
    		t7.start();
    		t8.start();
    		t9.start();
    		t10.start();
    	}
    }
    class MyThread implements Runnable{
    	private int ticket = 1000;
    	public void run() {
    		for(int i=0;i<1000;i++) {
    			this.sale();
    		}
    	} 
    	public synchronized void sale() {
    		if(ticket>0){
    			try {
    				Thread.sleep(300);
    			}catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			System.out.println(Thread.currentThread().getName()+"卖票:剩下="+ticket--);
    		}
    	}
    }
    


    总结:Thread是一个线程操作类,可以产生许多的线程,并且可以同时工作。

  • 相关阅读:
    WML
    CocoaPods install 安装
    刷新控件,审核
    UIImageView
    课堂练习——返回一个一维整数数组中最大子数组的和
    团队与题目介绍
    学习进度条05
    软件工程结对作业02
    课堂练习——返回一个整数数组中最大子数组的和
    学习进度条04
  • 原文地址:https://www.cnblogs.com/he932206959/p/11738599.html
Copyright © 2011-2022 走看看