• 买票问题


    
    /**
     * 买票问题
     */
    public class ThreadTrain1 implements Runnable {
        private int count = 10;
        private static Object o = new Object();
        @Override
        public void run() {
            while (count>0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sale();
            }
        }
    
        private void sale() {
            // 前提 多线程进行使用、多个线程只能拿到一把锁。
            // 保证只能让一个线程 在执行 缺点效率降低
            synchronized (o){
                if (count>0){
                    System.out.println(Thread.currentThread().getName()+",出售第"+(10-count+1)+"张票");
                    count--;
                }
            }
        }
        public static void main(String[] args){
            ThreadTrain1 train1 = new ThreadTrain1();
            Thread t1 = new Thread(train1,"1号窗口");
            Thread t2 = new Thread(train1,"2号窗口");
            t1.start();
            t2.start();
        }
    }
    
    
    /**
     * 多个线程想要同步,必须用同一把锁
     */
    public class ThreadTrain1 implements Runnable {
        private int count = 10;
        private static Object o = new Object();
        public boolean flag = true;
        @Override
        public void run() {
            if (flag){
                synchronized (this){
                    while (count>0){
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (count>0){
                            System.out.println(Thread.currentThread().getName()+",出售第"+(10-count+1)+"张票");
                            count--;
                        }
                    }
                }
            } else {
                sale();
            }
        }
    
        private synchronized void sale() { //非静态同步函数用的是this锁
            while (count>0){
                if (count>0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+",出售第"+(10-count+1)+"张票");
                    count--;
                }
            }
        }
        public static void main(String[] args) throws InterruptedException{
            ThreadTrain1 train1 = new ThreadTrain1();
            Thread t1 = new Thread(train1,"1号窗口");
            Thread t2 = new Thread(train1,"2号窗口");
            t1.start();
            Thread.sleep(40);
            train1.flag = false;
            t2.start();
        }
    }
    
  • 相关阅读:
    [Windows] 输入字符间距变宽
    [Android] 安卓手机不用root删除自带app
    [Linux] 内核通知链 notifier
    [RK3399] ES8316+NS4150 播放视频只有背景音,播放歌曲有的有声音,有的无声音
    [Linux] RTC 读写指令及测试程序
    [Linux] scp指令用法
    [Ubuntu] sudo apt-get update指令执行失败
    [RK3399] 修改移动网络默认为4G
    [RK3288] 外接USB设备出现丢数
    laravel 模拟数据批量添加
  • 原文地址:https://www.cnblogs.com/fly-book/p/11439328.html
Copyright © 2020-2023  润新知