今天学习ReentrantLock工具类,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent.locks;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import org.junit.Test;
/**
* ReentrantLock的测试类
*
* @author zqw
* @date 2020-06-21 20:46:26
*/
public class ReentrantLockTest {
class TestReentrantLockInnerClass extends Thread{
ReentrantLock lock=null;
public TestReentrantLockInnerClass(String name,ReentrantLock lock){
super(name);
this.lock=lock;
}
@Override
public void run() {
lock.lock();
try {
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
* 无参构造函数,默认非公平锁,效率高
* 非公平锁抢不到则不执行,不如队列
* void
* @Param
* @author zhqwm
* @date 2020/6/21 23:21
*/
@Test
public void testConstruct0()throws Exception{
ReentrantLock lock=new ReentrantLock();
TestReentrantLockInnerClass lockTest=new TestReentrantLockInnerClass("lock",lock);
TestReentrantLockInnerClass lockTest1=new TestReentrantLockInnerClass("lock1",lock);
lockTest.start();
Thread.sleep(2000);
lockTest1.start();
}
/**
* 有参构造函数,指定公平锁和非公平锁
* 公平锁抢不到的线程入队等等执行
* void
* @Param
* @author zhqwm
* @date 2020/6/21 23:21
*/
@Test
public void testConstruct1()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass lockTest=new TestReentrantLockInnerClass("lock",lock);
TestReentrantLockInnerClass lockTest1=new TestReentrantLockInnerClass("lock1",lock);
lockTest.start();
lockTest1.start();
}
/**
* lock和unlock必须同时使用
* void
* @Param
* @author zhqwm
* @date 2020/6/21 23:21
*/
@Test
public void testLock()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass lockTest=new TestReentrantLockInnerClass("lock",lock);
TestReentrantLockInnerClass lockTest1=new TestReentrantLockInnerClass("lock1",lock);
lockTest.start();
lockTest1.start();
}
class TestReentrantLockInnerClass1 extends Thread{
ReentrantLock lock=null;
public TestReentrantLockInnerClass1(String name,ReentrantLock lock){
super(name);
this.lock=lock;
}
@Override
public void run() {
try {
lock.lockInterruptibly();
Thread.sleep(3000);
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
*
* 线程在请求lock并被阻塞时,如果被interrupt,则此线程会被唤醒并被要求处理InterruptedException
* 和lock使用类似
* void
* @Param
* @author zhqwm
* @date 2020/6/21 23:21
*/
@Test
public void testLockInterruptibly()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass1 lockTest=new TestReentrantLockInnerClass1("lock",lock);
lockTest.start();
lockTest.interrupt();
Thread.sleep(1000000);
}
class TestReentrantLockInnerClass2 extends Thread{
ReentrantLock lock=null;
public TestReentrantLockInnerClass2(String name,ReentrantLock lock){
super(name);
this.lock=lock;
}
@Override
public void run() {
try {
while (!lock.tryLock()){
}
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
* 尝试获取锁,如果获取成功,则返回true,如果获取失败(即锁已被其他线程获取),则返回false
* void
* @Param
* @author zhqwm
* @date 2020/6/21 23:59
*/
@Test
public void testTryLock()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass2 lockTest=new TestReentrantLockInnerClass2("lock",lock);
TestReentrantLockInnerClass2 lockTest1=new TestReentrantLockInnerClass2("lock1",lock);
lockTest.start();
lockTest1.start();
}
class TestReentrantLockInnerClass3 extends Thread{
ReentrantLock lock=null;
public TestReentrantLockInnerClass3(String name,ReentrantLock lock){
super(name);
this.lock=lock;
}
@Override
public void run() {
try {
while (!lock.tryLock(3000, TimeUnit.MILLISECONDS)){
}
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
* 尝试获取锁3秒,如果获取成功,则返回true,如果超过3秒还没获取到,获取失败(即锁已被其他线程获取),则返回false
* void
* @Param
* @author zhqwm
* @date 2020/6/21 23:59
*/
@Test
public void testTryLock1()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass3 lockTest=new TestReentrantLockInnerClass3("lock",lock);
TestReentrantLockInnerClass3 lockTest1=new TestReentrantLockInnerClass3("lock1",lock);
lockTest.start();
lockTest1.start();
}
/**
* 释放锁
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:02
*/
@Test
public void testUnlock()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass3 lockTest=new TestReentrantLockInnerClass3("lock",lock);
TestReentrantLockInnerClass3 lockTest1=new TestReentrantLockInnerClass3("lock1",lock);
lockTest.start();
lockTest1.start();
}
class TestReentrantLockInnerClass4 extends Thread{
ReentrantLock lock=null;
Condition condition=null;
public TestReentrantLockInnerClass4(String name,ReentrantLock lock,Condition condition){
super(name);
this.lock=lock;
this.condition=condition;
}
@Override
public void run() {
lock.lock();
try {
System.out.println("name:"+getName());
condition.await();
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
class TestReentrantLockInnerClass5 extends Thread{
ReentrantLock lock=null;
Condition condition=null;
public TestReentrantLockInnerClass5(String name,ReentrantLock lock,Condition condition){
super(name);
this.lock=lock;
this.condition=condition;
}
@Override
public void run() {
lock.lock();
try {
System.out.println("name:"+getName());
condition.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
* condition 等待和释放
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:02
*/
@Test
public void testNewCondition()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
Condition condition=lock.newCondition();
TestReentrantLockInnerClass4 lockTest=new TestReentrantLockInnerClass4("lock",lock,condition);
TestReentrantLockInnerClass5 lockTest1=new TestReentrantLockInnerClass5("lock1",lock,condition);
lockTest.start();
lockTest1.start();
}
class TestReentrantLockInnerClass6 extends Thread{
ReentrantLock lock=null;
public TestReentrantLockInnerClass6(String name,ReentrantLock lock){
super(name);
this.lock=lock;
}
@Override
public void run() {
lock.lock();
try {
System.out.println(lock.getHoldCount());
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
* 查询当前线程保持锁定的个数,也就是调用lock()方法的次数
* 注意:当前线程
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testGetHoldCount()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass6 lockTest=new TestReentrantLockInnerClass6("lock",lock);
lockTest.start();
}
/**
* 查询持有锁的线程是否是当前线程
* 注意:当前线程
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testIsHeldByCurrentThread()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
lock.lock();
System.out.println(lock.isHeldByCurrentThread());
}
/**
* 是否锁定,state!=0
* 注意:当前线程
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testIsLocked()throws Exception{
ReentrantLock testObj=new ReentrantLock();
testObj.lock();
System.out.println(testObj.isLocked());
}
/**
* 是否是公平锁
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testIsFair()throws Exception{
ReentrantLock testObj=new ReentrantLock(true);
System.out.println(testObj.isFair());
}
/**
* 等待队列中是否还有线程
* 即head!=tail
* true 有 false 没有
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testHasQueuedThreads()throws Exception{
ReentrantLock testObj=new ReentrantLock();
System.out.println(testObj.hasQueuedThreads());
}
/**
* 参数线程是否在等待队列中
* 即head!=tail
* true 有 false 没有
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testHasQueuedThread()throws Exception{
ReentrantLock lock=new ReentrantLock(true);
TestReentrantLockInnerClass6 lockTest=new TestReentrantLockInnerClass6("lock",lock);
lockTest.start();
System.out.println(lock.hasQueuedThread(lockTest));
}
/**
* 获取等待获取锁定的线程数
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testGetQueueLength()throws Exception{
ReentrantLock testObj=new ReentrantLock();
System.out.println(testObj.getQueueLength());
}
class TestReentrantLockInnerClass7 extends Thread{
ReentrantLock lock=null;
public TestReentrantLockInnerClass7(String name,ReentrantLock lock){
super(name);
this.lock=lock;
}
@Override
public void run() {
lock.lock();
try {
System.out.println(lock.hasWaiters(lock.newCondition()));
System.out.println("name:"+getName());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/**
* w.waitStatus == Node.CONDITION
* 是否有等待线程
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testHasWaiters()throws Exception{
ReentrantLock testObj=new ReentrantLock(true);
TestReentrantLockInnerClass7 test=new TestReentrantLockInnerClass7("lock7",testObj);
test.start();
}
/**
* w.waitStatus == Node.CONDITION
* 获取cond.await中的线程数量
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testGetWaitQueueLength()throws Exception{
ReentrantLock testObj=new ReentrantLock();
testObj.lock();
System.out.println(testObj.getWaitQueueLength(testObj.newCondition()));
}
/**
* Object.toString[锁定状态 by 线程名称]
* void
* @Param
* @author zhqwm
* @date 2020/6/22 0:20
*/
@Test
public void testToString()throws Exception{
ReentrantLock testObj=new ReentrantLock();
testObj.lock();
System.out.println(testObj.toString());
}
}