isFair()、isHeldByCurrentThread()、isLocked()方法的使用

/ 多线程 / 没有评论 / 575浏览

isFair():作用是判断ReentrantLock是否是公平锁。返回true为公平锁,false为非公平锁。

/**
* 用户登录
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:35
* @since 1.0.0
*/
public class Userinfo {

private ReentrantLock lock;

public Userinfo(boolean fair) {
lock = new ReentrantLock(fair);
}

public void test() {
lock.lock();
System.out.println(String.format("是否是公平锁:%s", lock.isFair()));
lock.unlock();
}
}
/**
* 管理用户请求
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:44
* @since 1.0.0
*/
public class RequestAdmin extends Thread {

private Userinfo userinfo;

public RequestAdmin(Userinfo userinfo) {
this.userinfo = userinfo;
}

@Override
public void run() {
userinfo.test();
}
}
/**
* 用户请求
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:44
* @since 1.0.0
*/
public class RequestUser extends Thread {

private Userinfo userinfo;

public RequestUser(Userinfo userinfo) {
this.userinfo = userinfo;
}

@Override
public void run() {
userinfo.test();
}
}
/**
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-20 13:35
* @since 1.0.0
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
Userinfo userinfo = new Userinfo(true);
RequestAdmin requestAdmin = new RequestAdmin(userinfo);
requestAdmin.start();
userinfo = new Userinfo(false);
RequestUser requestUser = new RequestUser(userinfo);
requestUser.start();

}
}
是否是公平锁:true
是否是公平锁:false

isHeldByCurrentThread():查询当前线程是否保持锁。

/**
* 用户登录
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:35
* @since 1.0.0
*/
public class Userinfo {

private ReentrantLock lock;

public Userinfo(boolean fair) {
lock = new ReentrantLock(fair);
}

public void test() {
System.out.println(String.format("是否保持锁:%s", lock.isHeldByCurrentThread()));
lock.lock();
System.out.println(String.format("是否保持锁:%s", lock.isHeldByCurrentThread()));
lock.unlock();
}
}
/**
* 管理用户请求
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:44
* @since 1.0.0
*/
public class RequestAdmin extends Thread {

private Userinfo userinfo;

public RequestAdmin(Userinfo userinfo) {
this.userinfo = userinfo;
}

@Override
public void run() {
userinfo.test();
}
}
/**
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-20 13:35
* @since 1.0.0
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
Userinfo userinfo = new Userinfo(true);
RequestAdmin requestAdmin = new RequestAdmin(userinfo);
requestAdmin.start();
}
}
是否保持锁:false
是否保持锁:true

isLocked():查询锁是否有线程保持。

/**
* 用户登录
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:35
* @since 1.0.0
*/
public class Userinfo {

private ReentrantLock lock;

public Userinfo(boolean fair) {
lock = new ReentrantLock(fair);
}

public void test() {
System.out.println(String.format("是否有线程保持锁:%s", lock.isLocked()));
lock.lock();
System.out.println(String.format("是否有线程保持锁:%s", lock.isLocked()));
lock.unlock();
}
}
/**
* 管理用户请求
*
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-15 10:44
* @since 1.0.0
*/
public class RequestAdmin extends Thread {

private Userinfo userinfo;

public RequestAdmin(Userinfo userinfo) {
this.userinfo = userinfo;
}

@Override
public void run() {
userinfo.test();
}
}
/**
* @author Sama
* @author admin@jilinwula.com
* @date 2017-03-20 13:35
* @since 1.0.0
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
Userinfo userinfo = new Userinfo(true);
RequestAdmin requestAdmin = new RequestAdmin(userinfo);
requestAdmin.start();
}
}
是否有线程保持锁:false
是否有线程保持锁:true