容桂网站制作公司系统类小说
news/
2025/9/24 17:35:33/
文章来源:
容桂网站制作公司,系统类小说,通辽做网站建设,工厂办公室简单装修类ReentrantLock具有完全互斥排他的效果#xff0c;即同一时间只有一个线程在执行ReentrantLock.lock()后面的代码。这样虽然保证了线程的安全性#xff0c;但是效率低下。JDK提供了ReentrantReadWriteLock读写锁#xff0c;使用它可以加快效率#xff0c;在某些不需要操作… 类ReentrantLock具有完全互斥排他的效果即同一时间只有一个线程在执行ReentrantLock.lock()后面的代码。这样虽然保证了线程的安全性但是效率低下。JDK提供了ReentrantReadWriteLock读写锁使用它可以加快效率在某些不需要操作实例变量的方法中完全可以使用读写锁ReemtrantReadWriteLock来提升该方法的运行速度。 读写锁表示有两个锁一个是读操作相关的锁也称为共享锁另一个是写操作相关的锁也叫排他锁。也就是多个读锁之间不互斥读锁与写锁互斥、写锁与写锁互斥。在没有线程Thread进行写入操作时进行读取操作的多个Thread都可以获取读锁而进行写入操作的Thread只有在获取写锁后才能进行写入操作。即多个Thread可以同时进行读取操作但是同一时刻只允许一个Thread进行写入操作。 1.读读共享 读锁与读锁可以共享这种锁一般用于只读操作不对变量进行修改操作。 package cn.qlq.thread.twelve;import java.util.concurrent.locks.ReentrantReadWriteLock;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import cn.qlq.thread.one.RunnableThread;public class Demo1 {private ReentrantReadWriteLock lock new ReentrantReadWriteLock();// 读写锁private static final Logger log LoggerFactory.getLogger(Demo1.class);private int i;public String readI() {try {lock.readLock().lock();// 占用读锁log.info(threadName - {} 占用读锁,i-{}, Thread.currentThread().getName(), i);Thread.sleep(2 * 1000);} catch (InterruptedException e) {} finally {log.info(threadName - {} 释放读锁,i-{}, Thread.currentThread().getName(), i);lock.readLock().unlock();// 释放读锁}return i ;}public static void main(String[] args) {final Demo1 demo1 new Demo1();Runnable runnable new Runnable() {Overridepublic void run() {demo1.readI();}};new Thread(runnable, t1).start();new Thread(runnable, t2).start();new Thread(runnable, t3).start();}} 结果: 18:27:20 [cn.qlq.thread.twelve.Demo1]-[INFO] threadName - t2 占用读锁,i-018:27:20 [cn.qlq.thread.twelve.Demo1]-[INFO] threadName - t1 占用读锁,i-018:27:20 [cn.qlq.thread.twelve.Demo1]-[INFO] threadName - t3 占用读锁,i-018:27:22 [cn.qlq.thread.twelve.Demo1]-[INFO] threadName - t3 释放读锁,i-018:27:22 [cn.qlq.thread.twelve.Demo1]-[INFO] threadName - t1 释放读锁,i-018:27:22 [cn.qlq.thread.twelve.Demo1]-[INFO] threadName - t2 释放读锁,i-0 2.写写互斥 写锁与写锁互斥这就类似于ReentrantLock的作用效果。 package cn.qlq.thread.twelve;import java.util.concurrent.locks.ReentrantReadWriteLock;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class Demo2 {private ReentrantReadWriteLock lock new ReentrantReadWriteLock();// 读写锁private static final Logger log LoggerFactory.getLogger(Demo2.class);private int i;public void addI() {try {lock.writeLock().lock();// 占用写锁log.info(threadName - {} 占用写锁,i-{}, Thread.currentThread().getName(), i);Thread.sleep(2 * 1000);i;} catch (InterruptedException e) {} finally {log.info(threadName - {} 释放写锁,i-{}, Thread.currentThread().getName(), i);lock.writeLock().unlock();// 释放写锁}}public static void main(String[] args) {final Demo2 demo1 new Demo2();Runnable runnable new Runnable() {Overridepublic void run() {demo1.addI();}};new Thread(runnable, t1).start();new Thread(runnable, t2).start();new Thread(runnable, t3).start();}} 结果:(从时间可以看出实现了互斥效果) 18:31:31 [cn.qlq.thread.twelve.Demo2]-[INFO] threadName - t1 占用写锁,i-018:31:33 [cn.qlq.thread.twelve.Demo2]-[INFO] threadName - t1 释放写锁,i-118:31:33 [cn.qlq.thread.twelve.Demo2]-[INFO] threadName - t2 占用写锁,i-118:31:35 [cn.qlq.thread.twelve.Demo2]-[INFO] threadName - t2 释放写锁,i-218:31:35 [cn.qlq.thread.twelve.Demo2]-[INFO] threadName - t3 占用写锁,i-218:31:37 [cn.qlq.thread.twelve.Demo2]-[INFO] threadName - t3 释放写锁,i-3 3.读写互斥 package cn.qlq.thread.twelve;import java.util.concurrent.locks.ReentrantReadWriteLock;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** 读写互斥* * author Administrator**/
public class Demo3 {private ReentrantReadWriteLock lock new ReentrantReadWriteLock();// 读写锁private static final Logger log LoggerFactory.getLogger(Demo3.class);private int i;public String readI() {try {lock.readLock().lock();// 占用读锁log.info(threadName - {} 占用读锁,i-{}, Thread.currentThread().getName(), i);Thread.sleep(2 * 1000);} catch (InterruptedException e) {} finally {log.info(threadName - {} 释放读锁,i-{}, Thread.currentThread().getName(), i);lock.readLock().unlock();// 释放读锁}return i ;}public void addI() {try {lock.writeLock().lock();// 占用写锁log.info(threadName - {} 占用写锁,i-{}, Thread.currentThread().getName(), i);Thread.sleep(2 * 1000);i;} catch (InterruptedException e) {} finally {log.info(threadName - {} 释放写锁,i-{}, Thread.currentThread().getName(), i);lock.writeLock().unlock();// 释放写锁}}public static void main(String[] args) throws InterruptedException {final Demo3 demo1 new Demo3();new Thread(new Runnable() {Overridepublic void run() {demo1.readI();}}, t1).start();Thread.sleep(1 * 1000);new Thread(new Runnable() {Overridepublic void run() {demo1.addI();}}, t2).start();}} 结果: 18:34:59 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t1 占用读锁,i-018:35:01 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t1 释放读锁,i-018:35:01 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t2 占用写锁,i-018:35:03 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t2 释放写锁,i-1 4.写读互斥 写锁与读锁也是互斥的。先占用写锁后读锁进行抢占也会等待写锁释放。 package cn.qlq.thread.twelve;import java.util.concurrent.locks.ReentrantReadWriteLock;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** 读写互斥* * author Administrator**/
public class Demo3 {private ReentrantReadWriteLock lock new ReentrantReadWriteLock();// 读写锁private static final Logger log LoggerFactory.getLogger(Demo3.class);private int i;public String readI() {try {lock.readLock().lock();// 占用读锁log.info(threadName - {} 占用读锁,i-{}, Thread.currentThread().getName(), i);Thread.sleep(2 * 1000);} catch (InterruptedException e) {} finally {log.info(threadName - {} 释放读锁,i-{}, Thread.currentThread().getName(), i);lock.readLock().unlock();// 释放读锁}return i ;}public void addI() {try {lock.writeLock().lock();// 占用写锁log.info(threadName - {} 占用写锁,i-{}, Thread.currentThread().getName(), i);Thread.sleep(2 * 1000);i;} catch (InterruptedException e) {} finally {log.info(threadName - {} 释放写锁,i-{}, Thread.currentThread().getName(), i);lock.writeLock().unlock();// 释放写锁}}public static void main(String[] args) throws InterruptedException {final Demo3 demo1 new Demo3();new Thread(new Runnable() {Overridepublic void run() {demo1.addI();}}, t2).start();Thread.sleep(1 * 1000);new Thread(new Runnable() {Overridepublic void run() {demo1.readI();}}, t1).start();}} 结果: 18:36:14 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t2 占用写锁,i-018:36:16 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t2 释放写锁,i-118:36:16 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t1 占用读锁,i-118:36:18 [cn.qlq.thread.twelve.Demo3]-[INFO] threadName - t1 释放读锁,i-1 总结: 读写、写读、写写都是互斥的而读读是异步非互斥的。 也就是只要有写锁的参与就会进行同步所以写锁也被称为排他锁读锁被称为共享锁。 转载于:https://www.cnblogs.com/qlqwjy/p/10158409.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/916025.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!