黑马程序员技术交流社区
标题: 火车站卖票的例子用实现Runnable接口代码体现 [打印本页]
作者: aiheima 时间: 2016-1-9 01:05
标题: 火车站卖票的例子用实现Runnable接口代码体现
火车站卖票的例子用实现Runnable接口
public class Demo1 {
public static void main(String[] args) {
MyTicket mt = new MyTicket();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}
class MyTicket implements Runnable {
private int tickets = 100;
@Override
public void run() {
while (true) {
synchronized (this) {
if (tickets <= 0) {
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "...这是第"
+ tickets-- + "号票");
}
}
}
}
作者: yehua1026 时间: 2016-1-9 01:11
哈哈哈哈
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |