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

    实验报告

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

    1)实验代码

    package text8;
    
    public class MyThread implements Runnable{
    
    	private int tickets=1000;
    	
    	public int getTickets() {
    		return tickets;
    	}
    
    	public void setTickets(int tickets) {
    		this.tickets = tickets;
    	}
    
    	public void run() {
    		while(true) {
    			synchronized(this){
    				try {
    					if(tickets>0) {
    						System.out.println(Thread.currentThread().getName()+":是第 "+tickets+" 张票 ");
    						tickets--;
    					}
    					Thread.sleep(1000);
    				}catch(Exception e) {
    					System.out.println(e.getMessage());
    				}
    			}
    			if(tickets<=0){
    				break;
    			}
    		}
    	}
    
    }
    
    package text8;
    
    public class Text8 {
    
    	public static void main(String[] args) {
    		MyThread mt=new MyThread();
    		
    		new Thread(mt,"窗口1").start();
    		new Thread(mt,"窗口2").start();
    		new Thread(mt,"窗口3").start();
    		new Thread(mt,"窗口4").start();
    		new Thread(mt,"窗口5").start();
    		new Thread(mt,"窗口6").start();
    		new Thread(mt,"窗口7").start();
    		new Thread(mt,"窗口8").start();
    		new Thread(mt,"窗口9").start();
    		new Thread(mt,"窗口10").start();
    		
    	}
    
    }
    

    2)运行截图

    课程总结

    这周主要学习了多线程还学了一点关于Java的输入输出(主要是关于文件的)

    多线程

    Java中多线程的实现主要是通过继承Thread类或实现Runnable接口。其中Runnable接口可以资源共享。但不管使用哪种方法都要通过覆写run();在实例化的时候通过调用start()方法来启动多线程。

    还学习了一些实现多线程的相关方法,例如:
    Thread.getName();
    Thread.currentThread();
    isAlive(); 判断线程是否启动
    join(); 线程的强制运行
    Thread.sleep(); 线程的休眠
    interrupt(); 中断线程
    等等。。。

    还有哪个线程的优先级高,哪个线程有可能优先被执行。

    同步
    同步代码块

    synchronized(同步对象){        ///synchronized(this)this表示当前对象
            需要同步的代码块;
    }
    

    同步方法

    synchronized  方法返回值  方法名称(参数列表){
            方法体
    }
    

    使用File类进行文件的操作

    以下是针对文件本身操作的
    createNewFile(); 创建新文件
    delete(); 删除一个指定文件
    mkdir(); 创建一个文件夹
    public String[]list(); 列出全部名称,返回一个字符串数组
    public File[]listFiles(); 列完整的路径,返回一个File对象数组
    isDirectory(); 判断一个给定的路径是否是目录

    RandomAccessFile类可以对文件内容进行操作

  • 相关阅读:
    第一阶段冲刺4
    用户场景分析
    最小不重复数
    BOM
    虚拟机下ubuntu系统设置分辨率
    富文本编辑器KindEditor使用
    页面路径设置
    VMware虚拟机不能上网的问题
    Apache Tomcat/7.0.42配置用户
    JFreeChart 横轴文字竖着显示
  • 原文地址:https://www.cnblogs.com/H-Alice/p/11729349.html
Copyright © 2011-2022 走看看