多线程开发离不开锁机制,现在的Java语言中,提供了2种锁,一种是语言特性提供的内置锁,还有一种是 java.util.concurrent.locks 包中的锁,这篇文章简单整理一下内置锁的知识点。

内置锁在Java语言中的表现:

多线程的锁,其实本质上就是给一块内存空间的访问添加访问权限,因为Java中是没有办法直接对某一块内存进行操作的,又因为Java是面向对象的语言,一切皆对象,所以具体的表现就是某一个对象承担锁的功能,每一个对象都可以是一个锁。内置锁,使用方式就是使用 synchronized 关键字,synchronized 方法或者 synchronized 代码块。

每一种 synchronized 写法的锁是哪个对象:

1、指定当前对象加锁:

    private synchronized void function() {
//TODO execute something
}

2、指定当前类的Class对象加锁:

    private static synchronized void function() {
//TODO execute something
}

注意此处的 static 关键字。

3、指定任意对象加锁:

private void function() {
synchronized (object) {
//TODO execute something
}
}

此时,这段同步代码块的锁加在object对象上面。该对象可以是当前对象(object ==
this),也可以是当前类的Class对象(object == MyClass.class)。

简单验证一下:

现有如下的类:

public class SynchronizedTest {
private Object lock = new Object(); public void synchronizedBlockOnObject(long executeTime) {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " -> start synchronizedBlockOnObject");
doSomething(executeTime);
System.out.println(Thread.currentThread().getName() + " -> end synchronizedBlockOnObject");
}
} public void synchronizedBlockOnThis(long executeTime) {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " -> start synchronizedBlockOnThis");
doSomething(executeTime);
System.out.println(Thread.currentThread().getName() + " -> end synchronizedBlockOnThis");
}
} public void synchronizedBlockOnClass(long executeTime) {
synchronized (SynchronizedTest.class) {
System.out.println(Thread.currentThread().getName() + " -> start synchronizedBlockOnClass");
doSomething(executeTime);
System.out.println(Thread.currentThread().getName() + " -> end synchronizedBlockOnClass");
}
} public synchronized void synchronizedMethodOnThis(long executeTime) {
System.out.println(Thread.currentThread().getName() + " -> start synchronizedMethodOnThis");
doSomething(executeTime);
System.out.println(Thread.currentThread().getName() + " -> end synchronizedMethodOnThis");
} public static synchronized void synchronizedMethodOnClass(long executeTime) {
System.out.println(Thread.currentThread().getName() + " -> start synchronizedMethodOnClass");
doSomething(executeTime);
System.out.println(Thread.currentThread().getName() + " -> end synchronizedMethodOnClass");
} private static void doSomething(long executeTime) {
try {
Thread.sleep(executeTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

1、static synchronized 方法 和 synchronized (MyClass.class) {} 同步代码块的锁都加在
MyClass.class 对象上面:

public static void main(String[] args) {
SynchronizedTest synchronizedTest = new SynchronizedTest(); new Thread(new Runnable() {
@Override
public void run() {
SynchronizedTest.synchronizedMethodOnClass(3000);
}
}, "Thread static synchronized method").start();
new Thread(new Runnable() {
@Override
public void run() {
synchronizedTest.synchronizedBlockOnClass(2000);
}
}, "Thread synchronized block on Class").start();
}

运行结果如下:

Thread static synchronized method -> start synchronizedMethodOnClass
Thread static synchronized method -> end synchronizedMethodOnClass
Thread synchronized block on Class -> start synchronizedBlockOnClass
Thread synchronized block on Class -> end synchronizedBlockOnClass

说明当线程 Thread static synchronized method 进入方法 synchronizedMethodOnClass
的时候,线程Thread synchronized block on Class 是不能进入synchronizedBlockOnClass 代码块的。

2、非 static 的 synchronized 方法和 synchronized (this) {} 同步代码块的锁都加在当前对象上面:

public static void main(String[] args) {
SynchronizedTest synchronizedTest = new SynchronizedTest(); new Thread(new Runnable() {
@Override
public void run() {
synchronizedTest.synchronizedMethodOnThis(3000);
}
}, "Thread non-static synchronized method").start();
new Thread(new Runnable() {
@Override
public void run() {
synchronizedTest.synchronizedBlockOnThis(2000);
}
}, "Thread synchronized block on this").start();
}

运行结果如下:

Thread non-static synchronized method -> start synchronizedMethodOnThis
Thread non-static synchronized method -> end synchronizedMethodOnThis
Thread synchronized block on this -> start synchronizedBlockOnThis
Thread synchronized block on this -> end synchronizedBlockOnThis

说明当线程 Thread non-static synchronized method 进入方法 synchronizedMethodOnThis
的时候,线程Thread synchronized block on this 是不能进入synchronizedBlockOnThis 代码块的。

3、当锁加在 MyClass.class 、 this 、 任意对象,这三种情况,起不到任何同步作用:

    public static void main(String[] args) {
SynchronizedTest synchronizedTest = new SynchronizedTest(); new Thread(new Runnable() {
@Override
public void run() {
synchronizedTest.synchronizedMethodOnThis(3000);
}
}, "Thread non-static synchronized method").start();
new Thread(new Runnable() {
@Override
public void run() {
SynchronizedTest.synchronizedMethodOnClass(2000);
}
}, "Thread static sybchronized method").start();
new Thread(new Runnable() {
@Override
public void run() {
synchronizedTest.synchronizedBlockOnObject(4000);
}
}, "Thread sybchronized block on other Object").start();
}

运行结果如下:

Thread non-static synchronized method -> start synchronizedMethodOnThis
Thread static sybchronized method -> start synchronizedMethodOnClass
Thread sybchronized block on other Object -> start synchronizedBlockOnObject
Thread static sybchronized method -> end synchronizedMethodOnClass
Thread non-static synchronized method -> end synchronizedMethodOnThis
Thread sybchronized block on other Object -> end synchronizedBlockOnObject

说明当锁没有加在同一个对象上的时候,起不到线程间的同步作用。

Object中对内置锁进行操作的一些方法:

wait()系列:

wait()系列方法的作用是:使当前已经获得该对象锁的线程进入等待状态,并且释放该对象的锁。

notify()系列:

notify()系列方法的作用是:唤醒那些正在等待该对象锁的线程,使其继续运行。

基于wait() notify()机制,我们可以实现一个简易的生产者-消费者模型。

大体思路如下,一个生产者线程负责向一个仓库中存放(put)物品,一个消费者线程负责从仓库中取出(get)物品。

代码如下:

public class Warehouse {

    private Queue<Integer> queue;
private int capacity; public Warehouse(int capacity) {
this.capacity = capacity;
queue = new LinkedList();
} public synchronized void put(int num) {
if (queue.size() >= capacity) {
try {
System.out.println(Thread.currentThread().getName() + " , put full wait");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.add(num);
System.out.println(Thread.currentThread().getName() + " , put : " + num + " , queue -> " + queue);
notifyAll();
} public synchronized int get() {
if (queue.isEmpty()) {
try {
System.out.println(Thread.currentThread().getName() + " , get empty wait");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int num = queue.poll();
System.out.println(Thread.currentThread().getName() + " , get : " + num + " , queue -> " + queue);
notifyAll();
return num;
}
}
    public static void main(String[] args) {
Warehouse warehouse = new Warehouse(4);
Random random = new Random(); new Thread(new Runnable() {
@Override
public void run() {
while (true) {
warehouse.put(random.nextInt(10));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "生产者-01").start(); new Thread(new Runnable() {
@Override
public void run() {
while (true) {
warehouse.get();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "消费者-01").start();
}

运行结果如下:

生产者-01 , put : 5  , queue -> [5]
消费者-01 , get : 5 , queue -> []
生产者-01 , put : 7 , queue -> [7]
消费者-01 , get : 7 , queue -> []
生产者-01 , put : 9 , queue -> [9]
生产者-01 , put : 7 , queue -> [9, 7]
消费者-01 , get : 9 , queue -> [7]
生产者-01 , put : 0 , queue -> [7, 0]
生产者-01 , put : 5 , queue -> [7, 0, 5]
消费者-01 , get : 7 , queue -> [0, 5]
生产者-01 , put : 9 , queue -> [0, 5, 9]
生产者-01 , put : 6 , queue -> [0, 5, 9, 6]
消费者-01 , get : 0 , queue -> [5, 9, 6]
生产者-01 , put : 4 , queue -> [5, 9, 6, 4]
生产者-01 , put full wait
消费者-01 , get : 5 , queue -> [9, 6, 4]
生产者-01 , put : 6 , queue -> [9, 6, 4, 6]
生产者-01 , put full wait
消费者-01 , get : 9 , queue -> [6, 4, 6]
生产者-01 , put : 2 , queue -> [6, 4, 6, 2]
生产者-01 , put full wait
消费者-01 , get : 6 , queue -> [4, 6, 2]
生产者-01 , put : 9 , queue -> [4, 6, 2, 9]
生产者-01 , put full wait
消费者-01 , get : 4 , queue -> [6, 2, 9]
生产者-01 , put : 7 , queue -> [6, 2, 9, 7]
生产者-01 , put full wait
消费者-01 , get : 6 , queue -> [2, 9, 7]
生产者-01 , put : 2 , queue -> [2, 9, 7, 2]

Java内置锁的简单认识的更多相关文章

  1. Java内置锁和简单用法

    一.简单的锁知识 关于内置锁 Java具有通过synchronized关键字实现的内置锁,内置锁获得锁和释放锁是隐式的,进入synchronized修饰的代码就获得锁,走出相应的代码就释放锁. jav ...

  2. 深入理解Java内置锁和显式锁

    synchronized and Reentrantlock 多线程编程中,当代码需要同步时我们会用到锁.Java为我们提供了内置锁(synchronized)和显式锁(ReentrantLock)两 ...

  3. Java内置锁synchronized的实现原理

    简述Java中每个对象都可以用来实现一个同步的锁,这些锁被称为内置锁(Intrinsic Lock)或监视器锁(Monitor Lock). 具体表现形式如下: 1.普通同步方法,锁的是当前实例对象 ...

  4. 深入理解java内置锁(synchronized)和显式锁(ReentrantLock)

    多线程编程中,当代码需要同步时我们会用到锁.Java为我们提供了内置锁(synchronized)和显式锁(ReentrantLock)两种同步方式.显式锁是JDK1.5引入的,这两种锁有什么异同呢? ...

  5. Java内置锁synchronized的实现原理及应用(三)

    简述 Java中每个对象都可以用来实现一个同步的锁,这些锁被称为内置锁(Intrinsic Lock)或监视器锁(Monitor Lock). 具体表现形式如下: 1.普通同步方法,锁的是当前实例对象 ...

  6. Java 内置锁 重入问题

    阅读<Java并发编程实战>,遇到了一个问题 代码如下 /** * @auther draymonder */ public class Widget { public synchroni ...

  7. java内置锁实现锁住代码块方案(同一个对象或锁住整个类.class)

    我们看一个例子: class Demo { public synchronized void test() { System.out.println("test方法开始执行,当前线程为:&q ...

  8. Java内置锁synchronized的可重入性

    学习自 https://blog.csdn.net/aigoogle/article/details/29893667 对我很有帮助 感谢作者

  9. 七 内置锁 wait notify notifyall; 显示锁 ReentrantLock

    Object中对内置锁进行操作的一些方法: Java内置锁通过synchronized关键字使用,使用其修饰方法或者代码块,就能保证方法或者代码块以同步方式执行. 内置锁使用起来非常方便,不需要显式的 ...

随机推荐

  1. php单例模式的实现

    <?php /** * 设计模式之单例模式 * $_instance必须声明为静态的私有变量 * 构造函数和析构函数必须声明为私有,防止外部程序new * 类从而失去单例模式的意义 * getI ...

  2. vue 入门 ------简单购物车功能实现(全选,数量加减,价格加减)

    简易购物车功能(无任何布局 主要是功能) 数量的加减 商品的总价钱 全选与全不选 删除(全选.价格 受影响) <script src="https://cdn.jsdelivr.net ...

  3. 基于MR实现ngram语言模型

    在大数据的今天,世界上任何一台单机都无法处理大数据,无论cpu的计算能力或者内存的容量.必须采用分布式来实现多台单机的资源整合,来进行任务的处理,包括离线的批处理和在线的实时处理. 鉴于上次开会讲了语 ...

  4. ELF文件之九——使用链接脚本-2个函数-data-bss-temp-call-debug信息-struct

    main.c int enable; ; struct aaa { int membera; char memberb; }s_aaa; int main() { int temp; add(); d ...

  5. bootstrap组件---进度条

    <div class="progress"> <div class="progress-bar progress-bar-success" r ...

  6. 手动使用I2C协议写入24C02C

    刚尝试用AT89C52单片机使用IIC总线协议读写AT24C02C,我忽然想能否用手动调整开关的方式写入AT24C02C?于是,便尝试了一下,结果果然成功了. 关于IIC总线,这篇文章写的很详细:ht ...

  7. HTML连载69-透视属性以及其他属性练习

    一.透视属性 1.什么是透视 透视简单来说就是近大远小 2.​注意点:一定要注意,透视属性必须添加到需要呈现近大远小效果的​元素的父元素. 3.​格式:perspective:数字px; 这里的数字代 ...

  8. window 下如何恢复被删除的mysql root账户及密码(mysql 8.0.17)

    不久前自学完完sql,下了mysql8.0.17,安装配置好后探索着,想着用root账户登上去能不能删除root账户呢,然后就想给自己一巴掌,,, 如何快速恢复root: 1.关闭mysql服务:wi ...

  9. 2020数据字典php-直接复制

    [2020数据字典php-直接复制] <?php /** * 生成mysql数据字典 */ header ( "Content-type: text/html; charset=utf ...

  10. C++ Primer 抄书笔记(二)——变量和基本类型(上)

    一.基本内置类型 base build-in type[算数类型/类型转换/字面值常量] 基本内置类型(算数类型arithmetic type(整型integral type(字符,布尔bool),浮 ...