ReentrantLock是一个完全互斥排他的效果(和synchronized一样),但是这样有的时候会影响效率,就像两个线程同时读取一个数据,这样并不会产生线程不安全,如果使用ReentrantLock则会形成:先读线程一,然后读线程二,但是使用ReentrantReadWriteLock就可以避免这种情况。
读读共享:
package ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
//读读共享
public class Service {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void Method() {
try {
lock.readLock().lock();
System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得线程");
Thread.sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}finally {
lock.readLock().unlock();
}
}
}
package ReentrantReadWriteLock;
public class Run {
public static void main(String[] args) {
Service service = new Service();
Runnable runnable = new Runnable() {
public void run() {
service.Method();
}
};
Thread threadA = new Thread(runnable);
Thread threadB = new Thread(runnable);
threadA.setName("A");
threadA.start();
threadB.setName("B");
threadB.start();
}
}
//输出:
//线程B在1508554977877获得锁
//线程A在1508554977877获得锁
//几乎同一时间开始
写写互斥:
package ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
//写写互斥
public class Service2 {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void Method() {
try {
lock.writeLock().lock();
System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得线程");
Thread.sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}finally {
lock.writeLock().unlock();
}
}
}
package ReentrantReadWriteLock;
//写写互斥
public class Run2 {
public static void main(String[] args) {
Service2 service = new Service2();
Runnable runnable = new Runnable() {
public void run() {
service.Method();
}
};
Thread threadA = new Thread(runnable);
Thread threadB = new Thread(runnable);
threadA.setName("A");
threadA.start();
threadB.setName("B");
threadB.start();
}
}
//输出:
//线程A在1508555054030获得线程
//线程B在1508555064030获得线程
//这个大约中间间隔了10秒钟
读写互斥:
package ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Service3 {
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void read() {
try {
lock.readLock().lock();
System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得锁");
Thread.sleep(10000);
}catch(InterruptedException e) {
e.printStackTrace();
}finally {
lock.readLock().unlock();
}
}
public void write() {
try {
lock.writeLock().lock();
System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得锁");
Thread.sleep(10000);
}catch(InterruptedException e) {
e.printStackTrace();
}finally {
lock.writeLock().unlock();
}
}
}
package ReentrantReadWriteLock;
public class Run3 {
public static void main(String[] args) {
Service3 service = new Service3();
Runnable runnableRead = new Runnable() {
public void run() {
service.read();
}
};
Runnable runnablewrite = new Runnable() {
public void run() {
service.write();
}
};
Thread threadA = new Thread(runnableRead);
threadA.setName("A");
threadA.start();
Thread threadB = new Thread(runnablewrite);
threadB.setName("B");
threadB.start();
}
}
//输出:
//线程A在1508555152523获得锁
//线程B在1508555162524获得锁
//中间也间隔了10秒左右