1.getHoldCount方法
返回当前线程调用lock()方法的次数
package com.dome.lock.method;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
* getHoldCount
* @date 2021/3/11 14:16
*/
public class Test02 {
static ReentrantLock lock = new ReentrantLock();
public static void m1(){
try{
lock.lock();
//打印调用lock的次数
System.out.println(Thread.currentThread().getName()+"--hold count"+lock.getHoldCount());
//调用m2,ReentrantLock是可重入锁
m2();
}finally {
lock.unlock();
}
}
public static void m2(){
try{
lock.lock();
//打印调用lock的次数
System.out.println(Thread.currentThread().getName()+"==hold count"+lock.getHoldCount());
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
m1();
/*
main--hold count1
main==hold count
*/
}
}
二、getQueueLength方法
返回正等待获得锁的预估数
package com.dome.lock.method;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
* @date 2021/3/11 14:22
*/
public class Test03 {
static ReentrantLock lock = new ReentrantLock();
public static void sm(){
try{
lock.lock();
System.out.println(Thread.currentThread().getName()+"获得锁,执行方法,估计等待获得锁的线程数:" +lock.getQueueLength());
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
Test03.sm();
}
};
//开启10个线程
for (int i = 0; i < 10; i++) {
new Thread(r).start();
}
}
}
三、 getWaitQueueLength
返回与Condition条件相关的等待的线程预估数
package com.dome.lock.method;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
* getWaitQueueLength 返回与Condition条件相关的等待的线程预估数
* @date 2021/3/11 14:29
*/
public class Test04 {
static class Service{
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition(); //
public void waitMethod(){
try{
lock.lock();
System.out.println(Thread.currentThread().getName()+"进入等待前,现在该condition条件上等待的线程预估数"+
lock.getWaitQueueLength(condition));
condition.await();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void notifyMethod(){
try {
lock.lock();
condition.signalAll();
System.out.println("唤醒所有等待后,condition条件上等待的线程预估数"+lock.getWaitQueueLength(condition));
}finally {
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
Service service = new Service();
Runnable r = new Runnable() {
@Override
public void run() {
service.waitMethod();
}
};
//创建10个线程
for (int i = 0; i < 10; i++) {
new Thread(r).start();
}
Thread.sleep(1000);
service.notifyMethod();
}
}
四、hasQueuedThread
查询参数指定的线程是否在等待获得锁
hasQueuedThreads 查询是否还有线程等待获得该锁
package com.dome.lock.method;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
*
* hasQueuedThread 查询参数指定的线程是否在等待获得锁
*
* hasQueuedThreads 查询是否还有线程等待获得该锁
* @date 2021/3/11 14:38
*/
public class Test05 {
static ReentrantLock lock = new ReentrantLock();
public static void waitMethod(){
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + "获得了锁对象");
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
System.out.println(Thread.currentThread().getName() + "释放了锁对象----");
lock.unlock();
}
}
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable() {
@Override
public void run() {
Test05.waitMethod();
}
};
Thread[] threads = new Thread[6];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(r);
threads[i].setName("thread - "+i);
threads[i].start();
}
Thread.sleep(3000);
//判断数组中的每个线程对象是否正在等待获得锁
System.out.println(lock.hasQueuedThread(threads[0]));
System.out.println(lock.hasQueuedThread(threads[1]));
System.out.println(lock.hasQueuedThread(threads[2]));
System.out.println(lock.hasQueuedThread(threads[3]));
System.out.println(lock.hasQueuedThread(threads[4]));
Thread.sleep(2000);
//判断是否还有线程在等待获得该锁
System.out.println(lock.hasQueuedThreads());
}
}
五、hasWaiters
查询是否有线程正在等待指定的condition条件
package com.dome.lock.method;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
*
* hasWaiters 查询是否有线程正在等待指定的condition条件
* @date 2021/3/11 14:49
*/
public class Test06 {
static ReentrantLock lock = new ReentrantLock(); //锁
static Condition condition = lock.newCondition(); //返回锁 条件
static void sm(){
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + "lock .....");
System.out.println("是否有线程正在等待当点condition条件?"+lock.hasWaiters(condition) +"--等待数量: "
+lock.getWaitQueueLength(condition));
//超时后自动唤醒
condition.await(new Random().nextInt(1000), TimeUnit.MILLISECONDS);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
System.out.println(Thread.currentThread().getName() +"超时唤醒,是否有线程正在等待当点condition条件?"+lock.hasWaiters(condition) +"--等待数量: "
+lock.getWaitQueueLength(condition));
System.out.println(Thread.currentThread().getName() + "unlock...");
lock.unlock();
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
sm();
}
};
for (int i = 0; i < 10; i++) {
new Thread(r).start();
}
}
}
六、isHeldByCurrentThread
.isFair() 判断是否为公平锁
判断当前线程是否持有锁
package com.dome.lock.method;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
*
*
* .isFair() 判断是否为公平锁
*
* .isHeldByCurrentThread判断当前线程是否持有锁
* @date 2021/3/11 15:01
*/
public class Test07 {
static class Service{
private ReentrantLock lock;
public Service(boolean isFair){
this.lock = new ReentrantLock(isFair);
}
public void ServiceMethod(){
try{
System.out.println("是否公平锁? "+lock.isFair() + "---" +Thread.currentThread().getName() +
"调用lock前是否持有锁? "+lock.isHeldByCurrentThread());
lock.lock();
System.out.println(Thread.currentThread().getName() + "调用lock方法后是否持有锁? "+lock.isHeldByCurrentThread());
}finally {
if(lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
int num = new Random().nextInt();
new Service(num % 2 == 0 ? true : false).ServiceMethod();
}
};
for (int i = 0; i < 3; i++) {
new Thread(r,"thread-"+i).start();
}
}
}
七、 isLocked() 判断当前锁是否被线程持有
package com.dome.lock.method;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author qb
* @version 1.0
* @Description
* isLocked() 判断当前锁是否被线程持有
*
* @date 2021/3/11 15:09
*/
public class Test08 {
static ReentrantLock lock = new ReentrantLock();
static void sm(){
try {
System.out.println("lock方法之前--" + lock.isLocked());
lock.lock();
System.out.println("lock方法之后--" + lock.isLocked());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
if(lock.isHeldByCurrentThread()){
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("11 lock 是否被线程持有?---"+lock.isLocked());
new Thread(new Runnable() {
@Override
public void run() {
sm();
}
}).start();
Thread.sleep(3000);
System.out.println("22 lock 是否被线程持有?---"+lock.isLocked());
}
}