实验任务详情:

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

package test7;

public class Sellticket implements Runnable{
    private int number=1000;
    private int i=1;
    public void run() {
          while(true) {
            synchronized(this) {  
              if(number>0) {
                System.out.println(Thread.currentThread().getName()+"取得第"+(i++)+"张票"+"剩余"+(--number));
                if(number==0) {
                    System.out.println("票已售完");
                }
              }
                try {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e){
                    e.printStackTrace();
                }
           }
        }
     }
}
import java.util.Scanner;

public class Tickets {

    public static void main(String[] args) {
        String str[] = new String[10];
        String strs;
        Sellticket st1=new Sellticket();
        for(int i=0;i<str.length;i++) {
            Scanner in = new Scanner(System.in);
            strs = in.next();
              str[i]=strs;
        }
        for(int i=0;i<str.length;i++) {
            Thread t = new Thread(st1,str[i]);
            t.start();
        }
    }

}

学习总结:

(补:

死锁:

概念:所谓死锁,就是指两个线程都在等待彼此先完成,造成了程序的停滞状态。

注意:多个线程共享同一个资源时需要进行同步,以保证资源操作的完整性,但是过多的同步就有可能产生死锁。

)

一,线程的状态:

线程状态转换图:

(补:Thred类的定义:

public class Tread extends Object implements Runnable

从中得出Tread类是Runnable接口的子类。

)

1,Tread类的主要方法:

方法名称 类型 描述
public Tread(Runnable target) 构造 接收Runnable接口子类对象,实例化Tread对象
public Tread(Runnable target,String name) 构造 接收Runnable接口子类对象,实例化Tread对象,并设置线程名称
public Tread(String name) 构造 实例化Tread对象,并设置线程名称
public static Thread currentThread() 普通 返回目前正在执行的线程
public final String getName() 普通 返回线程的名称
public final int getPriority() 普通 发挥线程的优先级
public boolean isInterrupted() 普通 判断目前线程是否中断,如果是,返回true,否则,返回false
public final boolean isAlive() 普通 判断线程是否在活动,如果是,返回true;否则,返回false
public final void join() throws InterruptedException 普通 等待线程死亡
public final synachronized void join(long millis) throws InterruptedException 普通 等待millisecond毫秒后,线程死亡
public void run() 普通 执行线程
public final void setName(String name) 普通 设定线程名称
public final void setOrioity(int newPriority) 普通 设定线程的优先值
pulic static void sleep(long millis)throws InterruptedException 普通 使目前正在执行的线程休眠millis毫秒
public void start() 普通 开始执行线程
public String toString() 普通 返回代表线程的字符串
public Static void yield() 普通 将目前正在执行的线程暂停,允许其他线程执行
public final void setDaemon(boolean on) 普通 将一个线程设置成后台运行

2,线程的优先级

在java的线程操作中,所有的线程在运行前都会保持在就绪状态,此时,优先级高的线程先执行。

三种线程的优先级:

定义 描述 表示常量
public static final int MIN_PRIORITY 最低优先级 1
public static final int NORM_PRIORITY 中等优先级,是线程的默认优先级 5
public static final int MAX_PRIORITY 最高优先级 10

二,Java IO

1,File类的基本介绍:

在整个IO包中,唯一表示与文件本身有关的类就是File类。

File类的构造方法:

public File(String pathname)->实例化File类的时候,必须设置好路径

File类中的主要方法和常量:

方法或常量 类型 描述
pulic static final String pathSeparator 常量 表示路径的分隔符(windows是:“;”)
public static final String separator 常量 表示路径的分隔符(Windows是;“\”)
public File(String pathname) 构造 创建File类对象,传入完整路径
public File(File parent,String child) 构造 根据指定的父路径创建子类文件
public boolean creatNewFile() throws IOException 普通 创建新文件
public boolean delete() 普通 删除文件
public boolean exists() 普通 判断文件是否存在
public boolean isDirectory() 普通 判断给定的路径是否是在一个目录
public long length() 普通 返回文件的大小
public String[] list() 普通 列出指定目录的全部内容,只是列出了名称
public File[] listFiles() 普通 列出指定目录的全部内容,会列出路径
public boolean mkdit() 普通 创建一个目录
public boolean mkdirs() 普通 创建多级目录
public boolean renameTo(File dest) 普通 为己有的文件重命名
public long lastModified() 普通 取得文件的最后一次修改日期时间
public File getParentFile() 普通 取得当前路径的父路径

注意:

由于在Windows中使用反斜杠表示目录的分隔符:“\”,Linux中使用正斜杠表示目录的分隔符:“/”,为了满足这两个要求,可使用File类中提供的常量separator(使用格式:File.separator),除此之外还有一个常量pathSeparator(表示“;”)

一个文件的扩展名本身并没有任何意义,即不管有没有扩展名并不影响文件本身的内容(若文件有后缀名可与文件夹同名,否则,不可)

三,RandomAccessFile类

该类对文件的内容进行操作,此类属于随机读取类,可以随机读取一个文件中指定位置的数据。

常用操作方法:

方法 类型 描述
public RandomAccessFile(File file,String mode)throws FileNofoundException   接受File类的对象,指定操作路径,但是在设置是需要设置模式,“r”为只读,“w”为只写,“rw”为读写
public RandomAccessFile(String name,String mode)throws FileNofoundException   不在使用File类对象表示文件,而是直接输入了一个固定的文件路径
public void close() throws IOException   关闭操作
public int read(byte[] b)throws IOException   将内容读取到一个byte数组之中
public final byte readByte() throws IOException   读取一个字节
public final int readInt() throws IOException   从文件中读取整型数据
public void seek(long pos) throws IOException   设置读指针的位置
public final void writeBytes(String s)throws IOException   将一个字符串写入到文件值中,按字节的方式处理
public final void writeInt(int v)throws IOException   将一个int型数据写入文件,长度为4位
public int skipBytes(int n)throws IOException   指针跳过多少个字节