并发编程Synchronized锁

补充知识:

操作锁的原理有两种方式:

  1. 通过在内存上阻止多线程对对象的获取
  2. 在指令集上阻止多线程对指令的执行

Java中的每个对象都有一个monitor对象,加锁就是在竞争monitor对象,对代码块加锁是通过在指令前后添加monitorenter和monitorexit 指令实现的。

Synchronized 主要通过操作对象的monitor对象来实现锁的

  1. synchronized 作用于成员变量和非静态方法时,锁住的是对象实例
  2. synchronized  作用于静态方法时,锁住的是对应类,的Class实例对象
  3. synchronized  作用于一个代码块时,锁住的是所有代码块中配置的对象

// 作用于成员变量和非静态方法

一个对象

public class TestThread {

    public synchronized void thread1() throws InterruptedException {
        for(int i =0;i<10;i++){
            Thread.sleep(10);
            System.out.println("线程1");

        }


    }

    public synchronized void thread2() throws InterruptedException {
        for(int i =0;i<10;i++){
            Thread.sleep(10);
            System.out.println("线程2");

        }

    }

    public static void main(String[] args) {
        TestThread tt = new TestThread();
        ExecutorService es = Executors.newFixedThreadPool(5);
        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                tt.thread1();

            }
        });

        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                tt.thread2();
            }
        });
        es.shutdown();

    }
}

两个对象

public class TestThread {

    public synchronized   void thread1() throws InterruptedException {
        for(int i =0;i<10;i++){
            Thread.sleep(10);
            System.out.println("线程1");

        }


    }

    public synchronized   void thread2() throws InterruptedException {
        for(int i =0;i<10;i++){
            Thread.sleep(10);
            System.out.println("线程2");

        }

    }

    public static void main(String[] args) {
        TestThread tt = new TestThread();
        TestThread tt2 = new TestThread();
        ExecutorService es = Executors.newFixedThreadPool(5);
        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                tt.thread1();

            }
        });

        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                tt2.thread2();
            }
        });
        es.shutdown();

    }
}

// 作用于静态变量

public class TestThread {

    public static synchronized   void thread1() throws InterruptedException {
        for(int i =0;i<10;i++){
            Thread.sleep(10);
            System.out.println("线程1");

        }


    }

    public static synchronized   void thread2() throws InterruptedException {
        for(int i =0;i<10;i++){
            Thread.sleep(10);
            System.out.println("线程2");

        }

    }

    public static void main(String[] args) {
        TestThread tt = new TestThread();
        TestThread tt2 = new TestThread();
        ExecutorService es = Executors.newFixedThreadPool(5);
        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                thread1();

            }
        });

        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                thread2();
            }
        });
        es.shutdown();

    }
}

作用于代码块

public class TestThread {

    private static String a = "aa";

    public    void thread1() throws InterruptedException {

        synchronized (a){
            for(int i =0;i<10;i++){
                Thread.sleep(10);
                System.out.println("线程1");

            }
        }



    }

    public  synchronized   void thread2() throws InterruptedException {
        synchronized (a){
            for(int i =0;i<10;i++){
                Thread.sleep(10);
                System.out.println("线程2");

            }
        }


    }

    public static void main(String[] args) {
        TestThread tt = new TestThread();
        TestThread tt2 = new TestThread();
        ExecutorService es = Executors.newFixedThreadPool(5);
        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                tt.thread1();

            }
        });

        es.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                tt2.thread2();
            }
        });
        es.shutdown();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值