lockInterruptibly()、tryLock()方法的使用

lockInterruptibly():如果线程未被中断则正常获取锁,如果线程被中断则抛出异常并释放锁。

/**
 * 用户登录
 *
 * @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 test() {
        try {
            lock.lockInterruptibly();
            System.out.println(String.format("thread: %s", Thread.currentThread().getName()));
        } 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 requestAdmin = new RequestAdmin(userinfo);
        requestAdmin.start();
        RequestAdmin requestAdmin2 = new RequestAdmin(userinfo);
        requestAdmin2.start();
    }
}
thread: Thread-0
thread: Thread-1

我们看输出没有任何问题原因是线程没有被中断,如果我们将其中一个线程设置为中断,上述代码则会抛出异常。

/**
 * @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();
        RequestAdmin requestAdmin2 = new RequestAdmin(userinfo);
        requestAdmin2.start();
        requestAdmin2.interrupt();
    }
}
java.lang.InterruptedException
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1220)
	at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
	at Userinfo.test(Userinfo.java:17)
	at RequestAdmin.run(RequestAdmin.java:19)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
	at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
	at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
	at Userinfo.test(Userinfo.java:22)
	at RequestAdmin.run(RequestAdmin.java:19)
thread: Thread-0

tryLock():获取锁时返回true,获取不到时返回false。可通过返回结果处理相应的逻辑。

/**
 * 用户登录
 *
 * @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 test() {
        if(lock.tryLock()) {
            System.out.println(String.format("获得锁thread: %s", Thread.currentThread().getName()));
        } else {
            System.out.println(String.format("没有获得锁thread: %s", Thread.currentThread().getName()));
        }
    }
}
/**
 * 管理用户请求
 *
 * @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 requestAdmin = new RequestAdmin(userinfo);
        requestAdmin.start();
        RequestAdmin requestAdmin2 = new RequestAdmin(userinfo);
        requestAdmin2.start();
    }
}
获得锁thread: Thread-0
没有获得锁thread: Thread-1
Java

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

2017-3-21 22:31:20

Java

ReentrantReadWriteLock类的使用

2017-3-22 7:55:39

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索