getHoldCount()、getQueueLength()、getWaitQueueLength()方法的使用

释放双眼,带上耳机,听听看~!

getHoldCount():查询当前线程保持锁定的个数,也就是调用lock()方法的次数。

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

    private ReentrantLock lock = new ReentrantLock();

    public void test1() {
        lock.lock();
        System.out.println(String.format("getHoldCount: %s	thread: %s", lock.getHoldCount(), Thread.currentThread().getName()));
        test2();
        lock.unlock();
    }

    public void test2() {
        lock.lock();
        System.out.println(String.format("getHoldCount: %s	thread: %s", lock.getHoldCount(), Thread.currentThread().getName()));
        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.test1();
    }
}
/**
 * @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();
        RequestAdmin requestAdmin = new RequestAdmin(userinfo);
        requestAdmin.start();

    }
}
getHoldCount: 1	thread: Thread-0
getHoldCount: 2	thread: Thread-0

getQueueLength():返回正等待获取此锁定线程数,如果一共开启了5个线程,一个线程执行了await()方法,那么在调用此方法是,返回4,说明此时正有4个线程在等待锁的释放。

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

    public ReentrantLock lock = new ReentrantLock();

    public void test() {
        try {
            lock.lock();
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            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();
        RequestAdmin[] requestAdmins = new RequestAdmin[10];
        for (int i = 0; i < 10; i++) {
            requestAdmins[i] = new RequestAdmin(userinfo);
        }
        for (int i = 0; i < 10; i++) {
            requestAdmins[i].start();
        }
        Thread.sleep(1000);
        System.out.println(String.format("有%S个线程在等待锁", userinfo.lock.getQueueLength()));
    }
}
有9个线程在等待锁

getWaitQueueLength:返回同一个Condition类的等待线程数。如果有同时开启了5个线程,都调用了await()方法,并且它们是用的同一个Condition类,那么在调用该方法是,返回值为5。

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

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

    public ReentrantLock lock = new ReentrantLock();

    public Condition condition = lock.newCondition();

    public void test() {
        try {
            lock.lock();
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void test2() {
        lock.lock();
        System.out.println(String.format("有%S个线程在等待", lock.getWaitQueueLength(condition)));
        lock.unlock();
    }
}
/**
 * @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();
        RequestAdmin[] requestAdmins = new RequestAdmin[10];
        for (int i = 0; i < 10; i++) {
            requestAdmins[i] = new RequestAdmin(userinfo);
        }
        for (int i = 0; i < 10; i++) {
            requestAdmins[i].start();
        }
        Thread.sleep(1000);
        userinfo.test2();
    }
}
有10个线程在等待
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧