线程同步实现售票

方式1

package com.zy.demo05;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Moive{
    String name;
    int total;
    
    public Moive(String name,int total) {
        super();
        this.total = total;
        this.name=name;
    }

}

class Sales implements Runnable{
    String name;
    Moive moive;
    public Sales(String name,Moive moive) {
        super();
        this.name = name;
        this.moive = moive;
    }

    @Override
    public void run() {
        while(moive.total>0){
            
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            synchronized (moive) {
                
                   if(moive.total>0){
                       moive.total--;
                       System.out.println(name+"卖出1张,还剩"+moive.total);
                   }
            }
            
        }
        

        }
    }
        
public class TestTicket {
    
    
    public static void main(String[] args) {
        Moive moive = new Moive("功夫熊猫",5000);
        Sales sale1 = new Sales("手机",moive);
        Sales sale2 = new Sales("电脑",moive);
        Sales sale3 = new Sales("售票厅",moive);
        Thread thread1 = new Thread(sale1);
        Thread thread2 = new Thread(sale2);
        Thread thread3 = new Thread(sale3);

        thread2.start();
        thread3.start();
        thread1.start();
    };




}

方式2

package com.zy.demo05;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestTicket02 {
  static int ticket=5000;//为什么一定要用static?

    public static void main(String[] args) {
         ExecutorService ftp = Executors.newFixedThreadPool(3);        
         ftp.submit(
                new Runnable() {
                    
                    @Override
                    public void run() {
                        while(true){
                            synchronized (A.getA()) {
                                if(ticket>0){
                                    ticket-=1;
                                    System.out.println("手机卖出一张,还剩"+ticket);
                                }
                                
                                
                                
                            }
                        }
                        
                        
                    }
                }
                
                );
        
        ftp.submit(
                new Runnable() {
                    
                    @Override
                    public void run() {
                        while(true){
                            synchronized (A.getA()) {
                                if(ticket>0){
                                    ticket-=1;
                                    System.out.println("电脑卖出一张,还剩"+ticket);
                                }
                                
                                
                                
                            }
                        }
                        
                    }
                }
                
                );
        
        ftp.submit(
                new Runnable() {
                    
                    @Override
                    public void run() {
                        while(true){
                            synchronized (A.getA()) {
                                if(ticket>0){
                                    ticket-=1;
                                    System.out.println("售票厅卖出一张,还剩"+ticket);
                                }
                                
                                
                                
                            }
                        }
                        
                    }
                }
                
                );
    
    }

}

结果:截图一部分

注:方式2很少出现三个都抢到票的情况,上面为方式1的运行结果

posted @ 2019-06-26 22:19  勤奋的园  阅读(251)  评论(0编辑  收藏  举报