• 多线程(二)多线程安全与同步


    一,环境

      idea

    二.什么是线程安全问题,为什么会有线程安全问题

    线程安全问题产生于多个线程同时访问共享资源(通常查询不会产生)

    三.举例

    假如我现在想讲一个数循化加一,最终增加到1000.但是需要用5个线程来加

    class Count implements Runnable{
        private int count=1;
    
        public void run() {
            while (count<=1000){
                count=count+1;
                System.out.println(Thread.currentThread().getName()+",count:"+count);
            }
    
        }
    }
    public class ThreadTest {
        public static void main(String[] args) {
            Count  count=new Count();
            Thread t1=new Thread(count,"线程一");
            Thread t2=new Thread(count,"线程二");
            Thread t3=new Thread(count,"线程三");
            Thread t4=new Thread(count,"线程四");
            Thread t5=new Thread(count,"线程五");
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
        }
    
    }

    结果:

    代码显示:最多会增加到1000循环就会结束那么为什么会出现1001呢!!

    由于现在是多线程增加,有可能当count增加到999的时候同时又两个线程都进入了while循环里,然后就连续增加了两次

    那么怎么解决呢!!!

    四.使用锁来解决

    4.1同步代码块

    class Count implements Runnable{
        private volatile int  count=1;
        private static Object oj = new Object();
    
        public void run() {
            while (count<=1000){
                synchronized (oj) {//oj就是锁对象可以为任意对象也可为this
                    if(count<=1000) {
                        count = count + 1;
                        System.out.println(Thread.currentThread().getName() + ",count:" + count);
                    }
                }
            }
    
        }
    
    }
    public class ThreadTest {
        public static void main(String[] args) {
            Count  count=new Count();
            Thread t1=new Thread(count,"线程一");
            Thread t2=new Thread(count,"线程二");
            Thread t3=new Thread(count,"线程三");
            Thread t4=new Thread(count,"线程四");
            Thread t5=new Thread(count,"线程五");
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
        }
    
    }

    4.2使用同步方法

    class Count implements Runnable{
        private volatile int  count=1;
        private static Object oj = new Object();
    
        public void run() {
            while (count<=1000){
                
                sole();
                   
                }
            
    
        }
        public synchronized void sole(){//这把锁的锁对象就是this
            if(count<=1000) {
                count = count + 1;
                System.out.println(Thread.currentThread().getName() + ",count:" + count);
            }
        }
    }
    public class ThreadTest {
        public static void main(String[] args) {
            Count  count=new Count();
            Thread t1=new Thread(count,"线程一");
            Thread t2=new Thread(count,"线程二");
            Thread t3=new Thread(count,"线程三");
            Thread t4=new Thread(count,"线程四");
            Thread t5=new Thread(count,"线程五");
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
        }
    
    }
  • 相关阅读:
    Linux 用户 quote 配置 说明
    Linux 用户和用户组 配置说明
    ORA-00600: [kck_rls_check must use (11,0,0,0,0) or lower] 故障解决
    Oracle Data Guard PING[ARC2]: Heartbeat failed to connect to standby ''. Error is 12514 故障分析
    Oracle 11g RAC OCR 与 db_unique_name 配置关系 说明
    Data Guard 奇葩的 ORA-16191: Primary log shipping client not logged on standby 问题
    Oracle 11gR2 RMAN Duplicate 触发PLS-00201: identifier 'DBMS_RCVCAT.GETDBID' must be declared 错误
    RMAN RAC 到 单实例 duplicate 自动分配通道 触发 ORA-19505 错误
    Secure CRT 自动记录日志 配置 小记
    VBox fdisk 不显示 添加的硬盘 解决方法
  • 原文地址:https://www.cnblogs.com/wy0119/p/8999163.html
Copyright © 2020-2023  润新知