黄焖鸡米饭啊 2016-05-23 23:48 采纳率: 50%
浏览 1137

线程同步 卖票问题 三个车站卖七张票

正确的写法:public class mainclass {
public static void main(String[] args) {
Train A=new Train();
Train B=new Train();
Train C=new Train();
A.start();
B.start();
C.start();

}
}
class Train extends Thread{
public static int ticket=7;
public void run(){
{
A();
}
}
public synchronized void A(){
while (ticket>0){
System.out.println("第"+this.getName()+"站卖出第"+ticket--+"张票");
}
}

}
出现问题的写法:public class mainclass {
public static void main(String[] args) {
Train A=new Train();
Train B=new Train();
Train C=new Train();
A.start();
B.start();
C.start();

}
}
class Train extends Thread{
public static int ticket=7;
public void run(){
{
A();
}
}
public synchronized void A(){
while (ticket>0){
System.out.println("第"+this.getName()+"站卖出第"+ticket+"张票");
ticket--;
}
}

}
在A()函数之中ticket--的位置不同却出现了两种结果。一种将ticket--写在输出语句里,结果正确,但一种写在输出语句之外就会出现线程同步问题,可是我已经在函数前面加了synchronized,为什么会这样,按理来说两者应该一样啊。

  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2016-05-24 01:03
    关注

    首先,你的线程类Train类使用的public 方法的synchronized,锁匙当前this对象,所以你的三个线程都是使用各种的this对象,根本不存在锁竞争问题,都是各买各的票,彼此无影响的。
    其次,你想让三个线程卖票,有竞争的话,那么必须使用同一个锁对象,这个锁对象必须是从外界传递过去的。
    建议重新编写Train类,关联一个成员变量,然后同步使用这个锁对象,参考如下:

     public class Train extends Thread{
        private Ticket ticket;
    
        public Train(Ticket ticket){
            this.ticket = ticket;
        }
    
        public void run(){
            while(true){
                sale();
    
                //卖完一张票后休眠,让其他线程也卖
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public void sale(){
            synchronized (ticket) {
                int count = ticket.getCount();
                if(count<=0){
                    System.out.println("票已经卖完了啦。。。");
                    return;
                }
    
                System.out.println(Thread.currentThread().getName()+"售出一张票。");
            }
        }
    }
    
    

    Ticket类:

     public class Ticket {
        private int count;
        public Ticket(int count){
            this.count = count;
        }
        public int getCount() {
            return count;
        }
        public void setCount(int count) {
            this.count = count;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog