zoukankan      html  css  js  c++  java
  • 多线程同步标记

     1 package duoxiancheng;
     2 
     3 public class MaiPiao implements Runnable{
     4 
     5     //当前票数
     6     private int count=10;
     7     @Override
     8     public void run() {
     9         // TODO Auto-generated method stub
    10         
    11         
    12         // 卖票
    13         while(true)
    14         {
    15             //线程同步块
    16             synchronized ("同步标记:0;1") {
    17                 try {
    18                     Thread.sleep(100);
    19                 } catch (InterruptedException e) {
    20                     // TODO Auto-generated catch block
    21                     e.printStackTrace();
    22                 }
    23                 if(count>0){
    24                 count--;//卖出
    25                 System.out.println(Thread.currentThread().getName()+"剩余票数="+count);
    26                 }
    27                 else
    28                 {
    29                     break;
    30                 }
    31             }
    32             
    33             
    34             
    35         }
    36     }
    37     
    38     public static void main(String[] args)
    39     {
    40         MaiPiao m=new MaiPiao();
    41         
    42         Thread t1=new Thread(m,"售票点1");
    43         Thread t2=new Thread(m,"售票点2");
    44         Thread t3=new Thread(m,"售票点3");
    45         t1.start();
    46         t2.start();
    47         t3.start();
    48     }
    49     
    50     
    51 
    52 }
    View Code

  • 相关阅读:
    Hufman编码实现运用1 (原理不描述)
    E
    1178: [Apio2009]CONVENTION会议中心
    1071: [SCOI2007]组队
    #333. 【NOIP2017】宝藏
    CF 96 D. Volleyball
    CF 987 D. Fair
    qbxt的题:运
    qbxt的题:找一个三元环
    4361: isn
  • 原文地址:https://www.cnblogs.com/beens/p/5283259.html
Copyright © 2011-2022 走看看