小结:这周请了一天的假,所以回来的时候有些知识点跟不上,不过在第二节课学到了关于IO的知识很重要,对于这次的实验也有些吃力,这周的知识点主要集中在书上,在各种不同条件下学习运用什么样的代码。
一、File类
1.在整个io包中,唯一表示与文件本身有关的类就是File类。
2.使用File类可以进行创建或删除文件等常用操作。
3.要想使用File类,则首先要观察File类的构造方法。
二、File类中的主要方法和常量

实验任务详情:
完成火车站售票程序的模拟。
要求:
(1)总票数1000张;
(2)10个窗口同时开始卖票;
(3)卖票过程延时1秒钟;
(4)不能出现一票多卖或卖出负数号票的情况。
实验代码
package first;
class MyThread implements Runnable{
private int ticket = 1 ;
public void run(){
for(int i=0;i<1000;i++){
synchronized(this){
if(ticket<=1000){
try{
Thread.sleep(1000) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
System.out.println(Thread.currentThread().getName()+"卖票:ticket = " + ticket++ );
}
}
}
}
}
public class Demo{
public static void main(String args[]){
MyThread my = new MyThread();
Thread d1 = new Thread(my,"窗口A");
Thread d2 = new Thread(my,"窗口B");
Thread d3 = new Thread(my,"窗口C");
Thread d4 = new Thread(my,"窗口D");
Thread d5 = new Thread(my,"窗口E");
Thread d6 = new Thread(my,"窗口F");
Thread d7 = new Thread(my,"窗口G");
Thread d8 = new Thread(my,"窗口H");
Thread d9 = new Thread(my,"窗口I");
Thread d10 = new Thread(my,"窗口J");
d4.setPriority(Thread.MIN_PRIORITY) ;
d5.setPriority(Thread.MAX_PRIORITY) ;
d6.setPriority(Thread.NORM_PRIORITY) ;
d1.start() ;
d2.start() ;
d3.start() ;
d4.start() ;
d5.start() ;
d6.start() ;
d7.start() ;
d8.start() ;
d9.start() ;
d10.start() ;
}
}
实验结果
