学习笔记037——Java中【Synchronized锁】


实现线程同步,让多个线程排队依次获取某个资源,保证数据不会出错。

synchronized是Java中的关键字,是一种同步锁。它修饰的对象有以下几种:

  1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码块的对象;
  2. 修饰一个方法,被修饰的方法称为同步方法,其作用的范围是整个方法,作用的对象是调用这个方法的对象;
  3. 修改一个静态的方法,其作用的范围是整个静态方法,作用的对象是这个类的所有对象;
  4. 修改一个类,其作用的范围是synchronized后面括号括起来的部分,作用主的对象是这个类的所有对象。

1、修饰方法

1.1、静态方法,锁定的是类

package com.htl.Synchronized;

import java.util.concurrent.TimeUnit;

public class Test {

    public static void main(String[] args) {

        Fun fun1 = new Fun();
        Fun fun2 = new Fun();

        //线程A
        new Thread(new Runnable() {
            @Override
            public void run() {
                fun1.f1();
            }
        },"A线程").start();

        //休眠1秒
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //线程B
        new Thread(new Runnable() {
            @Override
            public void run() {
                fun2.f2();
            }
        },"线程B").start();

    }
}

class Fun{

    public synchronized static void f1(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("f1...");
    }

    public synchronized static void f2() {
        System.out.println("f2...");
    }

}

过3秒钟过后,f1、f2同时输出。

在这里插入图片描述

1.2、非静态方法,锁定的是方法的调用者(对象)

package com.htl.Synchronized;

import java.util.concurrent.TimeUnit;

public class Test {

    public static void main(String[] args) {

        Fun fun = new Fun();

        //线程A
        new Thread(new Runnable() {
            @Override
            public void run() {
                fun.f1();
            }
        },"A线程").start();

        //休眠1秒
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //线程B
        new Thread(new Runnable() {
            @Override
            public void run() {
                fun.f2();
            }
        },"线程B").start();

    }
}

class Fun{

    public synchronized void f1(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("f1...");
    }

    public synchronized void f2() {
        System.out.println("f2...");
    }

}

过3秒钟过后,f1、f2同时输出。

在这里插入图片描述

2、修饰代码块,锁定的是传入的对象

2.1、没有锁之前:

package com.htl.Synchronized;

import java.util.concurrent.TimeUnit;

public class Test2 {

    public static void main(String[] args) {

        Fun2 fun2 = new Fun2();

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    fun2.f1();
                }
            }).start();
        }

    }
}


class Fun2{

    public void f1(){
        System.out.println("start...");
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end...");
    }

}

先输出5个start… 过1秒再输出5个end…

在这里插入图片描述

2.2、有锁后:

package com.htl.Synchronized;

import java.util.concurrent.TimeUnit;

public class Test2 {

    public static void main(String[] args) {

        Fun2 fun2 = new Fun2();

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    fun2.f1();
                }
            }).start();
        }

    }
}


class Fun2{

    public void f1(){
        synchronized (this){
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end...");
        }
    }

}
package com.htl.Synchronized;

import java.util.concurrent.TimeUnit;

public class Test2 {

    public static void main(String[] args) {

        Fun2 fun2 = new Fun2();

        //开启5个线程
        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Integer num = 1;
                    fun2.f1(num);
                }
            }).start();
        }

    }
}


class Fun2{

    public void f1(Integer num){
        synchronized (num){
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end...");
        }
    }

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值