线程同步之synchronized

三个不安全的案例
package com.lean.syn;
//不安全的买票
//线程不安全有复数
public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();

        new Thread(buyTicket,"first").start();
        new Thread(buyTicket,"second").start();
        new Thread(buyTicket,"thrid").start();
    }
}
class BuyTicket implements Runnable{
    private int tickerNums = 10;
    private  boolean flag = true;//外部停止方式
    @Override
    public void run() {
        //买票
        while (flag){
            buy();
        }

    }
    private void buy(){
       //判断是否有票
        if(tickerNums<=0){
            flag = false;
            return ;
        }
        //模拟延迟
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //买票
        System.out.println(Thread.currentThread().getName()+"-->拿到了第"+tickerNums--+"张票");
    }
}
package com.lean.syn;

import java.lang.reflect.AccessibleObject;

//不安全的取钱
//两个人去银行取钱
public class UnsafeBank {
    public static void main(String[] args) {
        //账户
        Acount acount = new Acount(100, "结婚基金");

        Drawing you  = new Drawing(acount,50,"你");
        Drawing girlfriend  = new Drawing(acount,100,"女朋友");

        you.start();
        girlfriend.start();


    }
}


//账户
class Acount{
    int money;//余额
    String name;//卡明

    public Acount(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

//银行:模拟取款
class Drawing extends Thread{
    Acount acount;//账户
    //去了多少钱
    int drawingMoney;
    //现在手里有多少钱
    int nowMoney;

    public Drawing(Acount acount,int drawingMoney,String name){
        super(name);
        this.acount = acount;
        this.drawingMoney = drawingMoney;
    }
    //取钱
    @Override
    public void run() {
        //判断有没有钱
        if (acount.money-drawingMoney<0){
            System.out.println(Thread.currentThread().getName()+"钱不够取不了");
            return;
        }
        //放大问题的发生性
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //卡内余额
        acount.money = acount.money-drawingMoney;
        //手里的钱
        nowMoney = nowMoney+drawingMoney;

        System.out.println("账户余额"+acount.money);
        //this.getName = Thread.currentThread().getName()
        System.out.println(this.getName()+"手里的钱"+nowMoney);
    }
}
package com.lean.syn;

import java.util.ArrayList;
import java.util.List;

//不安全的集合
public class UnsafeList {
    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                list.add(Thread.currentThread().getName());
            }).start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}

同步方法
  • 由于我们可以通过private关键字来保证数据对象只能被方法访问,所以我们只需要针对方法提出一套机制,这套机制就是synchronized关键字,它包括两种用法:synchronized方法和synchronized块

同步方法:public synchronized void method(int args){}

  • synchronized方法控制对”对象"的当问,每个对象对应一把锁,每个synchronized方法都必须获得调用该方法的对象的锁才能指向,否则线程会阻塞,方法一旦执行,就独占该锁,一直到该方法返回才释放锁,后面被阻塞的线程才能获得这个锁继续执行

缺陷,若将一个大的方法申明为synchronized将会影响效率

  • 方法里面需要修改的内容才需要锁,锁的太多,浪费资源
package com.lean.syn;
//不安全的买票的修改:加上synchronized
//线程不安全有复数
public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();

        new Thread(buyTicket,"first").start();
        new Thread(buyTicket,"second").start();
        new Thread(buyTicket,"thrid").start();
    }
}
class BuyTicket implements Runnable{
    private int tickerNums = 10;
    private  boolean flag = true;//外部停止方式
    @Override
    public void run() {
        //买票
        while (flag){
            buy();
        }

    }
    //synchronized 同步方法,锁的是this
    private synchronized void buy(){
       //判断是否有票
        if(tickerNums<=0){
            flag = false;
            return ;
        }
        //模拟延迟
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //买票
        System.out.println(Thread.currentThread().getName()+"-->拿到了第"+tickerNums--+"张票");
    }
}

同步块

  • 同步块synchronized(Obj){}
  • Obj称之为 同步监视器
    • Obj 可以是任何对象,但是推荐使用共享资源作为同步监视器
    • 同步方法中无需同步监视器,因为同步方法的同步监视器就是this,就是这个对象本身,或者class
  • 同步监视器的执行过程
  1. 第一个线程访问,锁定同步监视器,执行其中代码

  2. 第二个线程访问,发现同步监视器被锁定,无法访问

  3. 第一个线程访问完毕,解锁同步监视器

  4. 第二个线程访问,发现同步监视器没有锁,然后锁定并访问

    package com.lean.syn;
    
    import java.lang.reflect.AccessibleObject;
    
    //不安全的取钱
    //两个人去银行取钱
    public class UnsafeBank {
        public static void main(String[] args) {
            //账户
            Acount acount = new Acount(1000, "结婚基金");
    
            Drawing you  = new Drawing(acount,50,"你");
            Drawing girlfriend  = new Drawing(acount,100,"女朋友");
    
            you.start();
            girlfriend.start();
    
    
        }
    }
    
    
    //账户
    class Acount{
        int money;//余额
        String name;//卡明
    
        public Acount(int money, String name) {
            this.money = money;
            this.name = name;
        }
    }
    
    //银行:模拟取款
    class Drawing extends Thread{
        Acount acount;//账户
        //去了多少钱
        int drawingMoney;
        //现在手里有多少钱
        int nowMoney;
    
        public Drawing(Acount acount,int drawingMoney,String name){
            super(name);
            this.acount = acount;
            this.drawingMoney = drawingMoney;
        }
        //取钱
        //synchronized默认锁的是this
        @Override
        public  void run() {
            //锁的对象就是变化的量,是需要增删改的对象
            synchronized (acount){
                //判断有没有钱
                if (acount.money-drawingMoney<0){
                    System.out.println(Thread.currentThread().getName()+"钱不够取不了");
                    return;
                }
                //放大问题的发生性
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //卡内余额
                acount.money = acount.money-drawingMoney;
                //手里的钱
                nowMoney = nowMoney+drawingMoney;
    
                System.out.println("账户余额"+acount.money);
                //this.getName = Thread.currentThread().getName()
                System.out.println(this.getName()+"手里的钱"+nowMoney);
            }
    
        }
    }
    
    package com.lean.syn;
    
    import java.util.ArrayList;
    import java.util.List;
    
    //不安全的集合
    public class UnsafeList {
        public static void main(String[] args){
            List<String> list = new ArrayList<>();
    
            for (int i = 0; i < 10000; i++) {
                new Thread(()->{
                    synchronized (list) {
                        list.add(Thread.currentThread().getName());
                    }
                }).start();
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(list.size());
        }
    }
    
    
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页