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

  • 相关阅读:
    CocosCreator-Widget,前后台切换
    Unity获取未激活游戏对象的方法 、坐标转换
    Mathf函数
    C# activeSelf、activeInHierarchy、SetActive、SetActiveRecursively
    C# 碰撞,射线,点击,周期函数等基本代码
    TCP/IP 协议栈
    笔记—《程序员自我修养》
    Container With Most Water 双指针法
    多线程服务器 编程模型
    c++ 高效并发编程
  • 原文地址:https://www.cnblogs.com/qfdy123/p/11094261.html
Copyright © 2011-2022 走看看