第九周课程总结&实验报告(七)

Java io
IO流用来处理设备之间的数据传输,Java程序中,对于数据的输入/输出操作 都是以“流”的方式进行的。java.io包下提供了各种“流”类的接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

流的分类
1.按照流的方向(输出输入都是站在程序所在内存的角度划分的)
1.输入流:只能从中读数据
2.输出流:只能向文件中写数据。

2.按照流的操作单元
1.字节流
2.字符流
3.节点流
4.处理流

� 提交作业

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

package nine;


 class Run implements Runnable {
    private int ticket=1000;
    public void run() {
        for(int i=0;i<10000;i++) {
            this.sale();
        }
    }
    public synchronized void sale() {
        if(ticket>0) {
                try {
                    Thread.sleep(1000);
                }catch(InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"卖票:ticket="+ticket--);
             }
       
       }
}


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

原文地址:https://www.cnblogs.com/shihao0701/p/11737390.html