lock.lock()之间的区别阻塞和同步阻塞 [英] Difference between the lock.lock() Blocking and Synchronized Blocking

查看:1175
本文介绍了lock.lock()之间的区别阻塞和同步阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们调用lock.lock()或尝试输入同步块,然后我们的线程块
如果一些其他线程已经采取该锁。现在我的问题是我们看看lock.lock()的
实现 - 它委托获取锁到AQS,它实际上驻留
当前线程(以便它不能被调度器进一步调度)。是同样的情况下同步阻塞吗?甚至我认为我的线程状态也不同。
ie如果我的线程被阻塞在同步块,它将是BLOCKING,而如果我调用
lock.lock(),那么它将是WAITING。我是对吗?

when we call either lock.lock() or try to enter a synchronized Block then our thread Blocks if some other thread has already taken that lock. Now My Question is we look at the implementation of lock.lock() - it delegates acquiring lock to AQS which actually parks the current thread (so that it cannot be scheduled further by scheduler). Is it the same case with synchronized blocking also?. Also even i think my thread status are also different. i.e If my thread is blocked on synchronized block it will be BLOCKING while if i have called lock.lock() then it will be WAITING. Am i right?

我的关注:

以下两种锁定策略之间的区别thread.status和停车而不是忙等待性能改进
ReentrantLock.lock();并同步{
/ * some code * /}

Difference between the below two locking strategies in aspects of thread.status and performance improvement by parking instead of busy waiting ReentrantLock.lock(); and synchronize { /*some code */}

推荐答案

BLOCKING - 被阻止在资源上,

BLOCKING - is blocked on a resource, cannot be interrupted

WAITING - 在资源上遭到封锁,但可能被中断或通知或取消预订。

WAITING - is blocked on a resource, but can be interrupted or notified or unparked.

看WAITING是更好的从另一个处理的控制。例如如果两个线程死锁,你可以打破一个带有中断的lock()。对于使用同步的双线程,您会卡住。

As you can see WAITING is better for control from another processed. e.g. if two threads are deadlocked you could break a lock() with an interrupt. With a two thread using synchronized you are stuck.

同步vs锁的行为非常相似,并且主要修订之间的确切细节会有所不同。

The behaviour of the synchronized vs lock is very similar and the exact details change between major revisions.

我的建议是使用


  • 同步更简单的代码,非常低的锁争用。

  • synchronized for simpler code where you need thread safety but have a very low lock contention.

使用Lock,您已经确定您有锁争用,或者您需要额外的功能,如tryLock。

use Lock where you have identified you have lock contention, or you need additional functionality like tryLock.

如果您

final Lock lock = new ReentrantLock();
lock.lock();
Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
});
t.start();
Thread.sleep(100);
System.out.println(t + " is " + t.getState());
lock.unlock();

print

Thread[Thread-0,5,main] is WAITING

Thread.State 状态

等待线程的线程状态。线程由于调用以下方法之一而处于等待状态:

Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:


  • Object.wait没有超时

  • Thread.join没有超时

  • LockSupport.park

在等待状态中等待另一个线程执行特定的动作。例如,在对象上调用Object.wait()的线程正在等待另一个线程调用该对象上的Object.notify()或Object.notifyAll()。一个调用Thread.join()的线程正在等待指定的线程终止。

A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

这篇关于lock.lock()之间的区别阻塞和同步阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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