首页 > 学院 > 开发设计 > 正文

Thread 使用Runnable接口模拟实现4个窗口售票

2019-11-14 21:36:13
字体:
来源:转载
供稿:网友
Thread 使用Runnable接口模拟实现4个窗口售票

//copyright?liupengcheng //http://www.VEVb.com/liupengcheng

/** * Created by Administrator on 2014/10/23. *以下事例模拟4个窗口售100张票 */

// 创建线程的另一种方法是声明实现 Runnable 接口的类。该类然后实现 run 方法。然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。

//copyright?liupengcheng //http://www.VEVb.com/liupengcheng

public class ThreadDemo5 {     public static void main(String [] args){         TestThread t = new TestThread();         new Thread(t).start();         new Thread(t).start();         new Thread(t).start();         new Thread(t).start();     } }

//copyright?liupengcheng //http://www.VEVb.com/liupengcheng

class TestThread implements Runnable{  //类调用Runnable 接口的类     PRivate int ticket = 100;     public void run()     {         while (true)         {             if(ticket>0)                 System.out.println(Thread.currentThread().getName()+"is sailing ticket"+ ticket--);         }     } }

//copyright?liupengcheng //http://www.VEVb.com/liupengcheng


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表