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

    实验七

    实验任务详情:

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

    实验代码

    package Work5;
    
    public 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--);
                 }          
           }
    }
    package Work5;
    
    public class test {
    	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();
        }
    }
    

    实验结果

    实验总结

    本周的习题在书本上有类似的题目,所以难度并不是很大,也很容易就完成了。

    课程总结

    多线程

    继承Thread类


    多线程的定义语句

    class 类名称 extends Thread{
    属性...;
    方法...;
    //覆写Thread中的run()方法,此方法是线程的主体
    public void run(){
      线程主体;
    }
    }
    

    启动线程的方法

    实现Runnable接口


    通过Runnable接口实现多线程

    class 类名称 implements Runnable{
    属性...;
    方法...;
    public void run(){
      //覆写Runnable接口里的run()线程主体;
    }
    }
    


    Java IO

    File类

    构造方法:
    public File(String pathname)
    File类的主要方法和变量

    操作延迟:

    总结:

    本周学了两个不同的知识点,多线程和Java IO,前者可以用来实现程序的多线程运行,比如用来买票;后者则是对电脑进行操作,创建,检查,删除文件等操作;都是面对对象进行编程,慢慢的知识点也变得有趣那么多了,希望保持这份热爱继续学下去。

  • 相关阅读:
    leetcode 14 Longest Common Prefix
    leetcode 20 Valid Parentheses 括号匹配
    leetcode 67 Add Binary
    ios swift 实现饼状图进度条,swift环形进度条
    【设计总结】DiDi
    【交互】退出弹窗思考
    【交互】导航栏设计
    【插件】Office
    【算法】聚类评价指标
    【Math】PCA与LDA
  • 原文地址:https://www.cnblogs.com/xzy999123/p/11736692.html
Copyright © 2011-2022 走看看