zoukankan      html  css  js  c++  java
  • 线程同步的诡异:求指点

    public class BUYER {
     public static void main(String[] args) { 
      TestThread a=new TestThread(); 
      TestThread B=new TestThread();
      a.setName("A");
      B.setName("B");
      a.start();  
      B.start();
     }
    }
    class TestThread extends Thread
    {
     static int Rest=10;//剩余票数
     int buy_1=7;//购买票数
     
     public void run(){
      int a=Rest;
      if(a>buy_1){ 
       a=a-buy_1; 

    Rest=a; //①为什么该语句放在这里永远无法出现同时购票成功的情况

       System.out.println(Thread.currentThread().getName()+" Success .");  
       //  ② Rest=a; 
        }else
       System.out.println(Thread.currentThread().getName()+" fail .");

    }
    }

    //①处售票的情况处理永是真确的.②处售票的情况处理可能出现同时成功的现象?

    知道何解的mankind求指点下.

  • 相关阅读:
    C#垃圾回收(GC)
    yum --enablerepo=elrepo-kernel install kernel-lt -y
    centos 查看版本
    linux 内核升级
    awk
    升级内核
    elerpo
    http://elrepo.org/tiki/tiki-index.php
    NO_TITLE
    MongoDB Find查询 1
  • 原文地址:https://www.cnblogs.com/be-the-one/p/4462603.html
Copyright © 2011-2022 走看看