zoukankan      html  css  js  c++  java
  • Java多线程小例子(三个窗口卖火车票)

    class Ticket implements Runnable{

     private int TicketNum = 100; //100张火车票
     private boolean flag = true;
     private synchronized void sale()
     {
      if(TicketNum<=0)
      {
       flag = false;
       return ;
      }
      TicketNum--;
      System.out.println(Thread.currentThread().getName()+"卖了一张票,还剩"+TicketNum+"张票。");
     }
     
     @Override
     public void run() {
      // TODO Auto-generated method stub
         
      while(flag)
      {
       sale();

       Java多线程小例子(三个窗口卖火车票)

       try {

        Thread.sleep(200);
       } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
     }
     
    }

    public class SaleTicket {

     public static void main(String[] args) {
      // TODO Auto-generated method stub
           Ticket t = new Ticket();
           Thread th1 = new Thread(t,"窗口A");
           Thread th2 = new Thread(t,"窗口B");
           Thread th3 = new Thread(t,"窗口C");
          
           th1.start();
           th2.start();
           th3.start();
          
     }

    }

  • 相关阅读:
    HDU 4285
    Codeforces 242C
    Codeforces 811C
    Codeforces 883H
    Codeforces 371D
    Codeforces 926E
    牛客算法周周练17 解题报告
    牛客算法周周练17D
    牛客算法周周练17C
    牛客算法周周练17A
  • 原文地址:https://www.cnblogs.com/dixinyunpan/p/5864773.html
Copyright © 2011-2022 走看看