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

    实验报告七

    完成火车站售票程序的模拟。

    要求:

    (1)总票数1000张;

    (2)10个窗口同时开始卖票;

    (3)卖票过程延时1秒钟;

    (4)不能出现一票多卖或卖出负数号票的情况。

    代码:

      package hello.java;
    
    public class Ticket  implements Runnable {
    
    private int ticket=1000;
    
    
    public int getTicket() {
        return ticket;
    }
    
    
     public void setTicket(int ticket) {
    this.ticket = ticket;
    }
    
    
     public void run()
     {
         for(int i=0;i<1000;i++)
        {
        synchronized(this)
        {
            if(ticket>0) {
            try {
                Thread.sleep(1000);
            }catch(InterruptedException ie)
            {
                ie.printStackTrace();
            }
             finally { System.out.println(Thread.currentThread().getName() + "卖票:ticket" +ticket-- );
              
              
              }
            
            }
        }
        }
    }
    }
    
            package hello.java;
    
        public class Demo06 {
    
        public static void main(String args[])
        {
      
       Ticket my = new Ticket();
    
      Thread t1=new  Thread(my,"窗口一");
      Thread t2=new  Thread(my,"窗口二");
      Thread t3=new  Thread(my,"窗口三");
      Thread t4=new  Thread(my,"窗口四");
      Thread t5=new  Thread(my,"窗口五");
      Thread t6=new  Thread(my,"窗口六");
      Thread t7=new  Thread(my,"窗口七");
      Thread t8=new  Thread(my,"窗口八");
      Thread t9=new  Thread(my,"窗口九");
      Thread t10=new Thread(my,"窗口十");
      
          t1.start();
          t2.start();
          t3.start();
          t4.start();
          t5.start();
          t6.start();
          t7.start();
         t8.start();
         t9.start();
         t10.start();
      
      
      }
    
    }
    

    心得:这题我照着书上P251页打的,自我感觉还行,不是很难,内心分分钟就接受了这个代码。

    第九周课程总结

    多线程:实现多线程代码有两种方式一种是继承Thread类;一种是实现runable接口

    继承Thread类

    class 类名称 extends  Thread{
         属性……;
        方法……;
        }
    public  void  run()
    {
    线程主体;
    }
    }
    

    用继承Thread类实现多线程时要注意正确地启动线程,是不能直接调用run()方法的,而是应该调用从Thread类中继承而来的start()方法

    实现Runnable接口

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

    无论使用哪种方法,最终都必须要用Thread类才能启动多线程

    线程的状态

    1.创建状态

    2.就绪状态

    3.运行状态

    4.堵塞状态

    5.死亡状态

    线程操作的相关方法

    取得和设置线程名称

    在Thread类中,可以通过getName()方法取得线程的名称,通过setName()方法设置线程的名称。

    另外如果出现没有为线程指定名称,则系统会自动的为线程分配一个名称

    判断线程是否启动

    通过Thread类之中的start()方法通知CPU这个线程已经准备好启动,之后就等待分配CPU资源,用isAlive()方法来测试线程是否已经启动,而且仍在启动

    线程强制运行

    在线程操作中,可以使用joio()方法让一个程序强制运行,线程强制运行期间,其他程序无法运行,必须等待此程序完成之后才能继续执行

    线程的休眠

    在程序中用Thread.sleep()方法使程序短暂休眠

    程序的中断

    interrupt()方法,中断程序运行状态

    程序的优先级

    线程的礼让

    在线程操作中,也可以用yield()方法将一个线程的操作暂时让给其他线程执行

    笔记

    继承Thread类不共享资源,Runnable接口共享资源

  • 相关阅读:
    队列<一>
    二叉树的遍历[先序,中序,后序]
    The method format(String, Object[]) in the type String is not applicable for the arguments
    sqlserver 备份 与 还原
    sqlserver 无法获得数据库独占权
    page.isvalid
    bzoj3994 [SDOI2015]约数个数和
    bzoj4868 [Shoi2017]期末考试
    bzoj1179 [Apio2009]Atm
    bzoj4869 [Shoi2017]相逢是问候
  • 原文地址:https://www.cnblogs.com/1793979463hyx/p/11739917.html
Copyright © 2011-2022 走看看