zoukankan      html  css  js  c++  java
  • 线程同步实现售票

    方式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的运行结果

  • 相关阅读:
    【算法总结】哈夫曼树
    堆栈入门-简单计算器
    堆栈入门-括号匹配问题
    Linux下使用vim命令编辑与修改文本内容
    Ubuntu查看与结束任务进程
    Mac OS X开发者准备工作
    Mac OS X平台上Java环境的配置
    OS X平台上MySQL环境搭建
    2015 Python News
    2015 Open Source News
  • 原文地址:https://www.cnblogs.com/qfdy123/p/11094261.html
Copyright © 2011-2022 走看看