线程完成时是否释放锁? [英] Does a thread release a lock when it finishes?

查看:116
本文介绍了线程完成时是否释放锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过一些地方的信息,如果没有将下面的代码包含在try...finally块中,则即使抛出异常,也可以释放该锁,这不是获取Lock对象的良好编程习惯.

这听起来像是一个简单的问题:线程完成后,属于该线程的所有锁会自动释放吗?

我问这个问题的原因是我正在处理的程序是这样的,即一旦线程获得了锁,它就没有理由放任它直到完成.另外,我是使用锁的新手,所以我想知道是否存在我可能没有考虑过的陷阱.我是否需要担心在线程完成之前在代码中显式释放锁,还是可以将其留给JVM,是否确信某些知识会在活动线程被激活后立即激活所有在活动线程锁上被阻塞的其他线程?线程停止了吗?

解决方案

简单测试可能会向您显示线程终止时不会释放锁:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    public static void main(String[] args) {
        final Lock l = new ReentrantLock();

        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+": Acquire lock");
                l.lock();
                System.out.println(Thread.currentThread()+": Lock aquired: wait");
                LockSupport.parkNanos(1_000_000_000);
                System.out.println(Thread.currentThread()+"; Exiting");
            }
        };
        t.start();
        LockSupport.parkNanos(500_000_000);
        System.out.println(Thread.currentThread()+": Acquire lock");
        l.lock();
        System.out.println(Thread.currentThread()+"; Success!");
    }
}

输出:

Thread[Thread-0,5,main]: Acquire lock
Thread[Thread-0,5,main]: Lock aquired: wait
Thread[main,5,main]: Acquire lock
Thread[Thread-0,5,main]; Exiting
// "Success" is never written: stuck in dead-lock

因此,当单独的线程获取锁然后退出时,主线程将无法获取锁.

I have read in places that it isn't good programming practice to acquire a Lock object without the code following being enclosed in a try...finally block so the lock can be released even if an exception is thrown.

It might sound like a simple question: do all locks belonging to a thread automatically release when the thread finishes?

The reason I ask this question is that the program I am working on is such that once a thread acquires a lock, it should have no reason let it go until it finishes. Additionally, I'm new to using locks, so I'm wondering if there's any pitfalls I may not have considered. Do I have to worry about explicitly releasing locks in my code before the thread finishes, or can I leave it to the JVM, confident in the certain knowledge that other threads being blocked on all the active thread's locks will be activated as soon as the active thread stops?

解决方案

Simple test may show you that lock is not released upon thread termination:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    public static void main(String[] args) {
        final Lock l = new ReentrantLock();

        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+": Acquire lock");
                l.lock();
                System.out.println(Thread.currentThread()+": Lock aquired: wait");
                LockSupport.parkNanos(1_000_000_000);
                System.out.println(Thread.currentThread()+"; Exiting");
            }
        };
        t.start();
        LockSupport.parkNanos(500_000_000);
        System.out.println(Thread.currentThread()+": Acquire lock");
        l.lock();
        System.out.println(Thread.currentThread()+"; Success!");
    }
}

Output:

Thread[Thread-0,5,main]: Acquire lock
Thread[Thread-0,5,main]: Lock aquired: wait
Thread[main,5,main]: Acquire lock
Thread[Thread-0,5,main]; Exiting
// "Success" is never written: stuck in dead-lock

So when the separate thread acquired the lock, then exited, lock cannot be taken by main thread.

这篇关于线程完成时是否释放锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆