Java提供另一机制去同步代码块。它比synchronized关键字更强大且易用。

它是基于Lock接口和其实现类例如:ReentrantLock。

这一机制对比synchronized关键字的优势在于:

1.可以构建更加灵活的同步代码块;

2.Lock接口提供了一些额外功能,例如tryLock()方法,它会返回一个布尔值以表示当前资源是否被锁。

3.Lock接口可以实现读写分离,多个读者但只有一个修改者。

4.Lock接口提供更好的性能。

本例中,我们将通过模拟打印队列去学习如何使用锁去同步代码块。

PrintQueue.java
package com.dylan.thread.ch2.c04.task;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /**
* This class simulates a print queue
*
*/
public class PrintQueue { /**
* Lock to control the access to the queue.
*/
private final Lock queueLock=new ReentrantLock(); /**
* Method that prints a document
* @param document document to print
*/
public void printJob(Object document){
queueLock.lock(); try {
Long duration=(long)(Math.random()*10000);
System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),(duration/1000));
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
queueLock.unlock();
}
}
}
Job.java
package com.dylan.thread.ch2.c04.task;

/**
* This class simulates a job that send a document to print.
*
*/
public class Job implements Runnable { /**
* Queue to print the documents
*/
private PrintQueue printQueue; /**
* Constructor of the class. Initializes the queue
* @param printQueue
*/
public Job(PrintQueue printQueue){
this.printQueue=printQueue;
} /**
* Core method of the Job. Sends the document to the print queue and waits
* for its finalization
*/
@Override
public void run() {
System.out.printf("%s: Going to print a document\n",Thread.currentThread().getName());
printQueue.printJob(new Object());
System.out.printf("%s: The document has been printed\n",Thread.currentThread().getName());
}
}
 
Main.java
package com.dylan.thread.ch2.c04.core;

import com.dylan.thread.ch2.c04.task.Job;
import com.dylan.thread.ch2.c04.task.PrintQueue; /**
* Main class of the example.
*
*/
public class Main { /**
* Main method of the class. Run ten jobs in parallel that
* send documents to the print queue at the same time.
*/
public static void main (String args[]){ // Creates the print queue
PrintQueue printQueue=new PrintQueue(); // Creates ten Threads
Thread thread[]=new Thread[10];
for (int i=0; i<10; i++){
thread[i]=new Thread(new Job(printQueue),"Thread "+i);
} // Starts the Threads
for (int i=0; i<10; i++){
thread[i].start();
}
} }

运行结果:

Thread 0: Going to print a document
Thread 5: Going to print a document
Thread 0: PrintQueue: Printing a Job during 0 seconds
Thread 8: Going to print a document
Thread 7: Going to print a document
Thread 3: Going to print a document
Thread 1: Going to print a document
Thread 4: Going to print a document
Thread 2: Going to print a document
Thread 9: Going to print a document
Thread 6: Going to print a document
Thread 0: The document has been printed
Thread 5: PrintQueue: Printing a Job during 7 seconds
Thread 5: The document has been printed
Thread 8: PrintQueue: Printing a Job during 0 seconds
Thread 7: PrintQueue: Printing a Job during 5 seconds
Thread 8: The document has been printed
Thread 7: The document has been printed
Thread 3: PrintQueue: Printing a Job during 7 seconds
Thread 3: The document has been printed
Thread 1: PrintQueue: Printing a Job during 3 seconds
Thread 1: The document has been printed
Thread 4: PrintQueue: Printing a Job during 7 seconds
Thread 4: The document has been printed
Thread 2: PrintQueue: Printing a Job during 5 seconds
Thread 9: PrintQueue: Printing a Job during 8 seconds
Thread 2: The document has been printed
Thread 9: The document has been printed
Thread 6: PrintQueue: Printing a Job during 4 seconds
Thread 6: The document has been printed

Java并发编程实例--16.使用ReentrantLock实现线程同步的更多相关文章

  1. java并发编程JUC第九篇:CountDownLatch线程同步

    在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口.ArrayBlockingQueue.DelayQueue.LinkedBlockingQueue.Priorit ...

  2. Java并发编程锁系列之ReentrantLock对象总结

    Java并发编程锁系列之ReentrantLock对象总结 在Java并发编程中,根据不同维度来区分锁的话,锁可以分为十五种.ReentranckLock就是其中的多个分类. 本文主要内容:重入锁理解 ...

  3. Java并发编程(二)如何保证线程同时/交替执行

    第一篇文章中,我用如何保证线程顺序执行的例子作为Java并发系列的开胃菜.本篇我们依然不会有源码分析,而是用另外两个多线程的例子来引出Java.util.concurrent中的几个并发工具的用法. ...

  4. 【Java并发编程实战】----- AQS(四):CLH同步队列

    在[Java并发编程实战]-–"J.U.C":CLH队列锁提过,AQS里面的CLH队列是CLH同步锁的一种变形.其主要从两方面进行了改造:节点的结构与节点等待机制.在结构上引入了头 ...

  5. 【Java并发编程实战】—– AQS(四):CLH同步队列

    在[Java并发编程实战]-–"J.U.C":CLH队列锁提过,AQS里面的CLH队列是CLH同步锁的一种变形. 其主要从双方面进行了改造:节点的结构与节点等待机制.在结构上引入了 ...

  6. Java并发编程实战.笔记十一(非阻塞同步机制)

    关于非阻塞算法CAS. 比较并交换CAS:CAS包含了3个操作数---需要读写的内存位置V,进行比较的值A和拟写入的新值B.当且仅当V的值等于A时,CAS才会通过原子的方式用新值B来更新V的值,否则不 ...

  7. 【Java并发编程】15、ReentrantLock实现原理深入探究

    原文已经写得非常详细了,直接把大神的文章转发过来了  https://www.cnblogs.com/xrq730/p/4979021.html 前言 这篇文章被归到Java基础分类中,其实真的一点都 ...

  8. Java并发编程实例(synchronized)

    此处用一个小程序来说明一下,逻辑是一个计数器(int i):主要的逻辑功能是,如果同步监视了资源i,则不输出i的值,但如果没有添加关键字synchronized,因为是两个线程并发执行,所以会输出i的 ...

  9. 那些年读过的书《Java并发编程实战》一、构建线程安全类和并发应用程序的基础

    1.线程安全的本质和线程安全的定义 (1)线程安全的本质 并发环境中,当多个线程同时操作对象状态时,如果没有统一的状态访问同步或者协同机制,不同的线程调度方式和不同的线程执行次序就会产生不同的不正确的 ...

  10. Java 并发编程中的 Executor 框架与线程池

    Java 5 开始引入 Conccurent 软件包,提供完备的并发能力,对线程池有了更好的支持.其中,Executor 框架是最值得称道的. Executor框架是指java 5中引入的一系列并发库 ...

随机推荐

  1. Nginx arm编译安装

    Nginx arm编译安装 背景 计划编译一套产品. 能够比较方便快捷的进行 nginx的交付. 主要思想是源码编译 不仅能够在arm上面运行 也可以在x86上面编译 考虑性能还有一些扩展性. 高效处 ...

  2. [转帖]xfs_repair命令详解

    https://bbs.qunyingkeji.com/2052/ 1.现状 目前网上出现大量的主机输入输出错误,原因是由于主机文件系统损坏.一线人员大部分采用的是umont 和 mount的方式恢复 ...

  3. [转帖]手把手教你在QEMU上运行RISC-V Linux

    https://kernel.0voice.com/forum.php?mod=viewthread&tid=3080   嵌入式Linux内核 发布于 2023-3-15 14:44:37  ...

  4. Springboot tomcat.threads线程数学习

    Springboot tomcat.threads线程数学习 摘要 压测完nginx 突然想搞一下springboot内嵌的tomcat的 threads的参数 一不做二不休, 直接就着脚本进行学习和 ...

  5. Docker 运行 Redis Rabbitmq seata-server ftp 的简单办法

    公司里面用到了很多组件, 发现安装二进制太麻烦了, 所以想用Docker 进行安装. 这里面简单给总结一下就不在折腾了.. 1. redis docker run -d -p 6379:6379 -- ...

  6. 在K8S中,Pod重启策略有哪些?

    在Kubernetes(简称K8s)中,Pod的重启策略定义了当容器失败时kubelet如何处理.有三种主要的重启策略: Always: 这是默认的重启策略.如果设置了为"Always&qu ...

  7. Protobuf示例:Golang and Python

    之前的文章中已经展示过如何在C++中使用protobuf,本文将简单示范protobuf在Golang和Python中的使用. Talk is cheap. Show you my code. 首先是 ...

  8. # 再次推荐github 6.7k star开源IM项目OpenIM性能测试及消息可靠性测试报告

    本报告主要分为两部分,性能测试和消息可靠性测试.前者主要关注吞吐,延时,同时在线用户等,即通常所说的性能指标.后者主要模拟真实环境(比如离线,在线,弱网)消息通道的可靠性. 先说结论,对于容量和性能: ...

  9. 宏观上看Spring创建对象的过程

    宏观上看Spring创建对象的过程 对于对象而言,可以分为简单对象和复杂对象: 简单对象 简单对象指可以直接new的对象: Spring在创建这些对象时,是基于反射来完成的. 复杂对象 复杂对象指不能 ...

  10. js下载附件(文件时候),文件名一直不出现乱码

    if (lastUrl.name) { //我们后端很奇怪有时候是个对象属性,有时候是个string window.open(url + `?attname=${lastUrl.name}`); } ...