黑马程序员技术交流社区

标题: 卖票系统 [打印本页]

作者: 等待未来    时间: 2016-9-24 22:05
标题: 卖票系统
package com.heima.testhomework;

public class Demo3 {
        public static void main(String[] args) {
                ticket t = new ticket();
                new Thread(t, "窗口a").start();
                new Thread(t, "窗口b").start();
                new Thread(t, "窗口c").start();
                new Thread(t, "窗口d").start();
        }
}


class ticket implements Runnable {
        private int num = 100;
        @Override
        public void run() {
                while (true) {
                        synchronized (this) {
                                if (num <= 0)
                                        break;
                                try {
                                        Thread.sleep(30);
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                System.out.println(Thread.currentThread().getName() + "第"
                                                + num-- + "张票");
                        }
                }
        }
}




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