黑马程序员技术交流社区

标题: 创建线程方法之 实现方式 Runnable 的 具体步骤演示 [打印本页]

作者: 德胜    时间: 2015-9-6 23:16
标题: 创建线程方法之 实现方式 Runnable 的 具体步骤演示
举例:
class Ticket implements Runnable   //1、定义类实现Runnable接口{
   private int tick=100;
   public void run()                             //实现Runnable接口中的方法
  {
   while(true)
    {
      if(tick〉0)
      {
       System.out.println(currentThread().getName()+“...sale”+tick--); //currentThread().getName()调用Thread默认名
       }      
   }
  }
}
class TicketDemo
{
  public static void main(String[] args)
  {
   Ticket t=new Ticket;                                                      //建立接口子类对象
   Thread t1=new Thread(t);                                             //建立Thread对象,将Runnable子类对象作为参数传进Thread对象
   Thread t2=new Thread(t);
   Thread t3=new Thread(t);
   Thread t4=new Thread(t);
    t1.start();                                                                      //调用Thread类的start方法,开启线程,并调用Runnable的子类中run方法
    t2.start();
    t3.start();
    t4.start();
    }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2