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

    一、实验内容

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

    实验源码

    Mythread类

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

    主类

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

    实验结果


    总结

    这周的题目比较简单,书上有实例可以参考。由于线程所包含的知识有点多,近期又没花很多时间去学习,所以就按书上的写了,有些问题还没搞懂,还要进一步学习。

    二、课程总结

    1、继承Thread类

    一个类只要继承了Thread类,此类就称为多线程操作类。

    class 类名称 extends Thread{
            属性···;
            方法···;
            public void run(){//在子类中需覆写的方法
                线程主体;
    }
    

    正确的启动线程应该从Thread类中继承而来的start()方法;一个类只能调用一次start()方法。

    2、实现Runnable接口

    class 类名称 implements Runnable{
        属性···;
        方法···;
        public void run{
                线程主体;
    }
    

    线程的启动需依靠Thread类完成。

    3、线程操作的主要方法

    序号 方法名称 类型 描述
    1 public Thread (Runnable target) 构造 接收Runnable接口子类对象,实例化Thread对象
    2 public Thread(Runnable target,String name) 构造 接收Runnable接口子类对象,实例化Thread对象,并设置线程名称
    3 pubic Thread(string name) 构造 实例化Thread对象,并设置线程名称
    4 public static Thread currentThread() 普通 返回目前正在执行的线程
    5 public final String getName() 普通 返回线程的名称
    6 public final int getPriority() 普通 发挥程序的优先级
    7 public boollean isInterruptd() 普通 判断目前线程是否被中断,如果是,返回true;否则,返回false
    8 public final boolean isAlive() 普通 判断线程是否在活动,如果是,返回true;否则,返回false
    9 public final void join() throws InterruptedException 普通 等待线程死亡
    10 public final Synchronized void join(long millis)throws InterruptedException 普通 等待millis毫秒后,死亡
    11 public void run() 普通 执行线程
    12 public final void setName(String name) 普通 设定线程的名称
    13 public final void setPriority(int newPriority) 普通 设定线程的优先级
    14 public static void sleep(long millis)throw InterruptedException 普通 使目前正在执行的线程休眠millis毫秒
    15 public void start() 普通 开始执行线程
    16 public String toString() 普通 返回代表线程的字符串
    17 public static void yield() 普通 将目前正在执行的线程暂停,允许其他线程执行
    18 public final void setDaemon(boolean on) 普通 将一个线程设置成后台运行

    4、同步

    synchronized(同步对象)//代码块
    {
            需同步的代码;
    }
    
     synchronized 方法返回值 方法名称(参数列表){
            //方法体
      }
    

    5、File类中主要方法和常量

    序号 方法或常量 类型 描述
    1 public static final String pathSeparator 常量 表示路径的分隔符";"
    2 public static final String separator 常量 表示路径的分隔符""
    3 pubic File(String pathname) 构造 创建file类对象,传入完整路径
    4 pubic File(File parent,String child) 构造 根据指定的夫路径创建子文件
    5 public boolean createNewFile()throws IOException 普通 创建文件
    6 public boolean delete() 普通 删除文件
    7 public boolean exists() 普通 判断文件是否存在
    8 public final boolean isDirectory() 普通 判断线程是否在活动,如果是,返回true;否则,返回false
    9 public long length() 普通 返回文件大小
    10 public Starting[] list[] 普通 列出指定目录的全部内容,只列出名称
    11 public File[] listFile[] 普通 列出路径
    12 public boolean mkdir() 普通 创建一个目录
    13 public boolean mkdirs() 普通 创建多级目录
    14 public boolean renameTo(File dest) 普通 为已有的文件重新命名
    15 public long lastModified 普通 取得文件的最后一次修改日期时间
    16 public File getParentFile() 普通 取得当前路径的夫路径
  • 相关阅读:
    数据结构进阶——线段树
    基本数据结构—Hash哈希
    NOIP2013提高组 day2 2019.7.15
    基本算法——归并排序
    基本数据结构—Trie
    NOIP 2011 提高组 Day2 校模拟 7.11
    Noip2014提高组真题Day1,2 校模拟7.7
    NOIP2015 提高组 day1 7.8校模拟
    NOIP2008 提高组 6.9校模拟
    STL-#include<set>
  • 原文地址:https://www.cnblogs.com/jk-liulei/p/11731735.html
Copyright © 2011-2022 走看看