java读写锁

----------------------------------------------------------------------------------------------------------------------------------加读写锁----------------------------------------------

package com.example.demo.util.dxc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* 读写锁 读读不互斥 读写互斥 写写互斥
* 保证读写操作共享数据线程安全
*
*/
public class TestReadWriteLock {
private static int num = 9;

public static void main(String[] args) {
ReentrantReadWriteLock reentrantLock = new ReentrantReadWriteLock();
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
get(reentrantLock);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
set(reentrantLock);
}
}).start();
}
}

/**
* 读读加锁的目的是 一个事务 简单理解为一个方法内不能出现不可重复读的情况 譬如 thread-2=3 thread-2=2 为
* 保证了一个事务能可重复读 读到的结果是一致的
* @param readWriteLock
*/
public static void get(ReadWriteLock readWriteLock) {
Lock lock = readWriteLock.readLock();
lock.lock();
System.out.println("read:" + Thread.currentThread().getName() + ":" + num);
System.out.println("read:" + Thread.currentThread().getName() + ":" + num);
lock.unlock();
}

public static void set(ReadWriteLock readWriteLock) {
Lock lock = readWriteLock.writeLock();
lock.lock();
num = num - 1;
System.out.println("write:" + Thread.currentThread().getName() + ":" + num);
lock.unlock();
}
}
read:Thread-0:9
read:Thread-0:9
read:Thread-2:9
read:Thread-2:9
write:Thread-1:8
write:Thread-3:7
read:Thread-4:7
read:Thread-4:7
write:Thread-5:6
read:Thread-6:6
read:Thread-6:6
write:Thread-7:5
read:Thread-8:5
read:Thread-8:5
read:Thread-10:5
read:Thread-10:5
read:Thread-12:5
read:Thread-12:5
write:Thread-11:4
write:Thread-9:3
read:Thread-14:3
read:Thread-14:3
write:Thread-15:2
read:Thread-16:2
read:Thread-16:2
write:Thread-19:1
write:Thread-13:0
write:Thread-17:-1
read:Thread-18:-1
read:Thread-18:-1
同一个线程 读读数据都一致


------------------------------------------------------------------------------------------------未加读写锁----------------------------------------------------------------
package com.example.demo.util.dxc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class TestUnReadWriteLock {
private static int num = 9;

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
get();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
set();
}
}).start();
}
}

public static void get() {
System.out.println("read:" + Thread.currentThread().getName() + ":" + num);
System.out.println("read:" + Thread.currentThread().getName() + ":" + num);
}

public static void set() {
num = num - 1;
System.out.println("write:" + Thread.currentThread().getName() + ":" + num);
}
}
read:Thread-0:9
read:Thread-0:9
write:Thread-1:8
read:Thread-2:8
read:Thread-2:8
write:Thread-3:7
read:Thread-4:7
read:Thread-6:6
read:Thread-6:6
write:Thread-7:5
write:Thread-5:6
read:Thread-4:5
read:Thread-10:5
read:Thread-10:4
write:Thread-13:3
read:Thread-8:5
read:Thread-8:3
write:Thread-15:2
write:Thread-11:4
write:Thread-17:1
read:Thread-12:1
read:Thread-12:1
read:Thread-16:1
read:Thread-16:1
read:Thread-18:1
read:Thread-18:1
write:Thread-9:0
write:Thread-19:-1
read:Thread-14:-1
同一个线程(红色)读读数据不一致

posted @ 2019-12-17 20:59  洞玄巅峰  阅读(311)  评论(0编辑  收藏  举报