售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。
- public class Demo01 {
- public static void main(String[] args) {
- new Ticket().start();
- new Ticket().start();
- new Ticket().start();
- new Ticket().start();
-
- }
- }
- class Ticket extends Thread {
- private static int ticket = 100;
- public void run() {
- while (true) {
- synchronized (Ticket.class) {
-
- if (ticket <= 0) {
- break;
- }
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- System.out.println(getName() + "...这是第" + ticket-- + "张票");
- }
- }
- }
- }
复制代码
|
|