tryLock方法 - 非阻塞方法? [英] tryLock method - non-blocking method?

查看:1124
本文介绍了tryLock方法 - 非阻塞方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tryLock 方法的文档说它是一个非阻塞方法

允许你获取/获取锁(如果是可以在调用方法时)。

The documentation of the tryLock method says that it is a non-blocking method
which allows you to obtain/acquire the lock (if that's possible at the time of calling the method).

但我想知道:你怎么能获得锁定并且仍然保证同时你的方法是$
tryLock )是非阻塞的?!获取锁意味着你是
试图访问一个受保护的代码部分,所以它应该阻止(如果你不幸运的话)
即你应该至少在某些情况下阻止)。任何人都可以解释这个逻辑吗?
?纯粹从逻辑的角度来看:我不太明白如何完成这一切(保证方法不会阻止)。除非他们在tryLock本身的代码中使用另一个

线程...

But I wonder: how can you obtain a lock and still guarantee at the same time that
your method (tryLock) is non-blocking?! Acquiring the lock implies that you're
trying to access a guarded section of code so it should block (if you're not lucky
i.e. you should block at least in certain scenarios). Could anyone explain the logic
behind this? Purely from a logical standpoint: I don't quite understand how this can
be done at all (guaranteeing that the method doesn't block). Unless they use another
thread of course within the code of the tryLock itself...

http://docs.oracle.com/javase/7/ docs / api / java / util / concurrent / locks / Lock.html#tryLock%28%29

推荐答案

这些机制的大多数实现使用所谓的CAS CPU指令来基于变量执行原子操作。 CAS表示比较和交换。它们会查看变量的值,如果它是您所期望的,则更改它。这提供了线程安全(非阻塞/锁定)方式来对多线程数据进行比较。

Most implementations of these mechanisms use socalled CAS CPU instructions to do atomic actions based on a variable. CAS means Compare and Swap. These look at the value of a variable and if it is what you expect you change it. This provides a threadsafe (non blocking/locking) way to do comparison on data that is multithreaded.

CAS指令以原子方式执行以下操作:

A CAS instruction does the following atomically:

private int stored = 0;
public int compareAndSwap(int expectedValue , int newValue)

   if(expectedValue == stored)
       stored = newValue;

   return stored;
}

这些非阻塞机制通常只是重试上述函数,直到成功为止(返回value是预期值)。因为重试循环非常短,每次迭代中线程中断的可能性很小(或者实际上OS调度程序甚至会使其无法实现)。

These non blocking mechanisms generally just retry the above function until it succeeds (the returned value is the expected value). Because the retry loop is very short the chances of a thread interrupting on each iteration are tiny (or in practice the OS scheduler will even make it impossible).

实际的java锁( Lock 只是它们实现的接口)都复杂得多,因为它们提供了额外的功能。但实质上CAS机制是大多数非阻塞线程安全类的基础。

The actual java locks (Lock is just the interface they implement) are all much more complex because they offer extra features. But in essence the CAS mechanism is the base for most non-blocking threadsafe classes.

如果你对锁定的内部工作感兴趣, Java Concurrency in Practice 是一个很好的资源。轻松地开始Java并发可以做什么,并推进它如何做到这一点。 (即使对于非Java程序员来说,它也是一个很好的来源)。你的问题在第15章处理。

If you are interested in the inner workings of locking, Java Concurrency in Practice is a great source. Starting gently with what Java concurrency can do and advancing in how it does it. (it is a great source even for non java programmers). Your question is handled in chapter 15.

这篇关于tryLock方法 - 非阻塞方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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