ReentrantLock也能夠讓代碼塊原子執(zhí)行,但是比synchronized更加強(qiáng)大,ReentrantLock具有嗅探鎖定琼蚯、多路分支通知等功能僵蛛。
嗅探鎖定:是指獲取鎖時(shí)如果鎖已經(jīng)被其他線程獲取到ReentrantLock可以進(jìn)行指定等待時(shí)間獲取鎖或者
多路分支通知:是指線程發(fā)生await時(shí),線程可以選擇注冊(cè)在不同的監(jiān)視器Condition對(duì)象上瑟幕,在適當(dāng)?shù)臅r(shí)候可以選擇指定的監(jiān)視器Condition對(duì)象上的線程進(jìn)行signal通知磕蒲、執(zhí)行
1、多線程執(zhí)行同一代碼塊互斥
import java.util.concurrent.locks.ReentrantLock;
class MyService {
private ReentrantLock lock = new ReentrantLock();
public void method() {
try {
lock.lock();
for (int i = 1; i <= 3; i++) {
Thread.sleep(1000);
System.out.println("ThreadName=" + Thread.currentThread().getName() + " " + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
class MyThread extends Thread {
private MyService service;
MyThread(MyService service) {
this.service = service;
}
@Override
public void run() {
service.method();
}
}
public class Test {
public static void main(String[] args) {
MyService service = new MyService();
MyThread myThread1 = new MyThread(service);
MyThread myThread2 = new MyThread(service);
myThread1.start();
myThread2.start();
}
}
結(jié)果
ThreadName=Thread-0 1
ThreadName=Thread-0 2
ThreadName=Thread-0 3
ThreadName=Thread-1 1
ThreadName=Thread-1 2
ThreadName=Thread-1 3
2只盹、多線程執(zhí)行不同代碼塊互斥
import java.util.concurrent.locks.ReentrantLock;
class MyService {
private ReentrantLock lock = new ReentrantLock();
public void methodA() {
lock.lock();
try {
System.out.println("MethodA begin ThreadName=" + Thread.currentThread().getName());
for (int i = 1; i <= 3; i++) {
System.out.println("ThreadName=" + Thread.currentThread().getName() + " " + i);
Thread.sleep(1000);
}
System.out.println("MethodA end ThreadName=" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void methodB() {
lock.lock();
try {
System.out.println("MethodB begin ThreadName=" + Thread.currentThread().getName());
for (int i = 1; i <= 3; i++) {
System.out.println("ThreadName=" + Thread.currentThread().getName() + " " + i);
Thread.sleep(1000);
}
System.out.println("MethodB end ThreadName=" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
class ThreadA extends Thread {
private MyService service;
ThreadA(MyService service) {
this.service = service;
}
@Override
public void run() {
service.methodA();
}
}
class ThreadB extends Thread {
private MyService service;
ThreadB(MyService service) {
this.service = service;
}
@Override
public void run() {
service.methodB();
}
}
public class Test {
public static void main(String[] args) {
MyService service = new MyService();
ThreadA threadA = new ThreadA(service);
threadA.setName("A");
ThreadB threadB = new ThreadB(service);
threadB.setName("B");
threadA.start();
threadB.start();
}
}
結(jié)果:
MethodA begin ThreadName=A
ThreadName=A 1
ThreadName=A 2
ThreadName=A 3
MethodA end ThreadName=A
MethodB begin ThreadName=B
ThreadName=B 1
ThreadName=B 2
ThreadName=B 3
MethodB end ThreadName=B
3辣往、多路通知
當(dāng)多線程進(jìn)入await狀態(tài)時(shí),利用Condition監(jiān)視器對(duì)不同類型線程通知
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
class MyService {
private ReentrantLock lock = new ReentrantLock();
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
public void methodA() {
lock.lock();
try {
System.out.println("MethodA begin ThreadName=" + Thread.currentThread().getName() + " " + System.currentTimeMillis());
conditionA.await();
System.out.println("MethodA end ThreadName=" + Thread.currentThread().getName() + " " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void methodB() {
lock.lock();
try {
System.out.println("MethodB begin ThreadName=" + Thread.currentThread().getName() + " " + System.currentTimeMillis());
conditionB.await();
System.out.println("MethodB end ThreadName=" + Thread.currentThread().getName() + " " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void signalA() {
lock.lock();
conditionA.signal();
lock.unlock();
}
public void signalA_All() {
lock.lock();
conditionA.signalAll();
lock.unlock();
}
public void signalB() {
lock.lock();
conditionB.signal();
lock.unlock();
}
public void signalB_All() {
lock.lock();
conditionB.signalAll();
lock.unlock();
}
}
class ThreadA extends Thread {
private MyService service;
ThreadA(MyService service) {
this.service = service;
}
@Override
public void run() {
service.methodA();
}
}
class ThreadB extends Thread {
private MyService service;
ThreadB(MyService service) {
this.service = service;
}
@Override
public void run() {
service.methodB();
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
ThreadA threadA = new ThreadA(service);
threadA.setName("A");
ThreadB threadB = new ThreadB(service);
threadB.setName("B");
threadA.start();
threadB.start();
Thread.sleep(1000);
service.signalA();
Thread.sleep(1000);
service.signalB();
// ThreadA[] threadAs = new ThreadA[10];
// for (int i=0;i<5;i++){
// threadAs[i] = new ThreadA(service);
// }
// ThreadB[] threadBs = new ThreadB[10];
// for (int i=0;i<5;i++){
// threadBs[i] = new ThreadB(service);
// }
//
// for (int i=0;i<5;i++){
// threadAs[i].start();
// threadBs[i].start();
// }
//
// Thread.sleep(1000);
// service.signalA_All();
// Thread.sleep(1000);
// service.signalB_All();
}
}
測(cè)試結(jié)果1:
MethodA begin ThreadName=A 1476800025028
MethodB begin ThreadName=B 1476800025029
MethodA end ThreadName=A 1476800026028
MethodB end ThreadName=B 1476800027028
修改代碼為:
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
// ThreadA threadA = new ThreadA(service);
// threadA.setName("A");
//
// ThreadB threadB = new ThreadB(service);
// threadB.setName("B");
// threadA.start();
// threadB.start();
//
// Thread.sleep(1000);
// service.signalA();
// Thread.sleep(1000);
// service.signalB();
ThreadA[] threadAs = new ThreadA[10];
for (int i=0;i<5;i++){
threadAs[i] = new ThreadA(service);
}
ThreadB[] threadBs = new ThreadB[10];
for (int i=0;i<5;i++){
threadBs[i] = new ThreadB(service);
}
for (int i=0;i<5;i++){
threadAs[i].start();
threadBs[i].start();
}
Thread.sleep(1000);
service.signalA_All();
Thread.sleep(1000);
service.signalB_All();
}
結(jié)果為:
MethodA begin ThreadName=Thread-0 1476800227416
MethodB begin ThreadName=Thread-7 1476800227417
MethodB begin ThreadName=Thread-5 1476800227417
MethodA begin ThreadName=Thread-3 1476800227418
MethodA begin ThreadName=Thread-1 1476800227418
MethodA begin ThreadName=Thread-4 1476800227418
MethodB begin ThreadName=Thread-6 1476800227418
MethodA begin ThreadName=Thread-2 1476800227418
MethodB begin ThreadName=Thread-8 1476800227418
MethodB begin ThreadName=Thread-9 1476800227418
MethodA end ThreadName=Thread-0 1476800228419
MethodA end ThreadName=Thread-3 1476800228419
MethodA end ThreadName=Thread-1 1476800228419
MethodA end ThreadName=Thread-4 1476800228420
MethodA end ThreadName=Thread-2 1476800228420
MethodB end ThreadName=Thread-7 1476800229419
MethodB end ThreadName=Thread-5 1476800229419
MethodB end ThreadName=Thread-6 1476800229419
MethodB end ThreadName=Thread-8 1476800229419
MethodB end ThreadName=Thread-9 1476800229419
4殖卑、公平所與非公平鎖
公平鎖是指線程獲(getting)取鎖的順序和線程鎖定(got)的順序相同站削,
Lock的一些方法說明
A) getHoldCount()
獲取當(dāng)前鎖定(got)的個(gè)數(shù)
B)getQueueLength()
獲取當(dāng)前正在等待獲取(getting)鎖的線程數(shù)(Threads)
C)getWaitQueueLength(Condition)
返回等待(await)與此鎖定(got)的Condition相關(guān)的個(gè)數(shù)
原文:https://blog.csdn.net/jihaitaowangyi/article/details/52852693