zoukankan      html  css  js  c++  java
  • Java-多线程-安全性-同步代码块技术

    1、不加同步技术模拟售票例子

    package cn.bruce.Thread;
    
    //多线程售票案例
    //解决安全问题,JAVA程序提供了同步技术synchronized(任意对象){线程要操作的共享数据}
    //同步代码块
    public class MoreThreadDemo2 {
        public static void main(String[] args) {
            // 创建Runnable接口实现类对象
            Tickets t = new Tickets();
            // 创建三个Thread类对象,传递Runnable接口实现类
            Thread t0 = new Thread(t, "窗口1");
            Thread t1 = new Thread(t, "窗口2");
            Thread t2 = new Thread(t, "窗口3");
            // 开启线程
            t0.start();
            t1.start();
            t2.start();
    
        }
    }
    
    class Tickets implements Runnable {
        private int number = 100;
        Object object = new Object();
    
        public void run() {
            while (true)
            {
                //synchronized(object){//同步技术不能使用匿名对象,并且速度会变慢
                    //对票数做判断,大于0 可以出售
                if (number > 0)
                {
                    try
                    {
                        Thread.sleep(10);//使用休眠,模拟CPU切换
                        System.out.println(Thread.currentThread().getName() + "出售第" + number--);
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
    
                }//}
            }
        }
    
    }

    结果会出现售-1的情况

     2、加入同步技术后

    package cn.bruce.Thread;
    
    //多线程售票案例
    //解决安全问题,JAVA程序提供了同步技术synchronized(任意对象){线程要操作的共享数据}
    //同步代码块
    public class MoreThreadDemo2 {
        public static void main(String[] args) {
            // 创建Runnable接口实现类对象
            Tickets t = new Tickets();
            // 创建三个Thread类对象,传递Runnable接口实现类
            Thread t0 = new Thread(t, "窗口1");
            Thread t1 = new Thread(t, "窗口2");
            Thread t2 = new Thread(t, "窗口3");
            // 开启线程
            t0.start();
            t1.start();
            t2.start();
    
        }
    }
    
    class Tickets implements Runnable {
        private int number = 100;
        Object object = new Object();
    
        public void run() {
            while (true)
            {
                synchronized(object){//同步技术不能使用匿名对象,并且速度会变慢
                    //对票数做判断,大于0 可以出售
                if (number > 0)
                {
                    try
                    {
                        Thread.sleep(10);//使用休眠,模拟CPU切换
                        System.out.println(Thread.currentThread().getName() + "出售第" + number--);
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
    
                }}
            }
        }
    
    }

    不会出现异常数据,但是速度会慢一些

     

  • 相关阅读:
    Django连接SQL Server,安装相关扩展包及相关配置
    安装pipenv
    报错及网络上的解决方案
    Nucleus-SE迁移:未实现的设施和兼容性
    RTOS诊断和错误检查
    Nucleus SE RTOS初始化和启动
    Nucleus 实时操作系统中断(下)
    Nucleus 实时操作系统中断(上)
    系统时间
    应用程序计时器
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13572244.html
Copyright © 2011-2022 走看看