多线程-实现Runnable接口的方式卖电影票案例

来源:互联网 发布:unity3d打包apk 编辑:程序博客网 时间:2024/06/05 02:13
package cn.itcast_07;public class SellTicket implements Runnable {// 定义100张票private int tickets = 100;@Overridepublic void run() {while (true) {if (tickets > 0) {System.out.println(Thread.currentThread().getName() + "正在出售第" + (tickets--) + "张票");}}}}
package cn.itcast_07;/* * 实现Runnable接口的方式实现 */public class SellTicketDemo {public static void main(String[] args) {// 创建资源对象SellTicket st = new SellTicket();// 创建三个线程对象Thread t1 = new Thread(st, "窗口1");Thread t2 = new Thread(st, "窗口2");Thread t3 = new Thread(st, "窗口3");// 启动线程t1.start();t2.start();t3.start();}}