English 中文(简体)
原子内核公平吗?
原标题:Is AtomicInteger fair?

http://docs.oracle.com/javase/6/docs/api/java/util/contic/atomic/AtomicInteger.html" rel=“nofollow” >AtomicInteger 是否提供了任何公平保障?像先到先得的线条执行顺序? 的动画例子http://sourcforge.net/projects/javaconsistra/"rel=nofol" >Victor Grazi s 并同时进行模拟 肯定没有表现出任何公平性。我搜索并找到任何结论。

最佳回答

没有这种保证,如果有的话,文件将对此作出说明。

当你考虑它时,AtomicInteger 基本上是一个围绕compare-and-swap (或类似的)。保证先到先得的语义将要求线条之间同步,这代价很高,而且与 AtomicInteger 的理念相反。

事情是这样的 如果有多个线条想要,比如, incriementAnget () 同一原子整数同时出现, 他们完成比赛的顺序不详 。

问题回答

与你可能做的其他事情相比,它的运作速度非常快,没有任何价值。 这意味着你极不可能得到争议,因此公平不可能成为一个问题。

如果你称它为20亿次(你可以用几秒钟做),它反正会过量流。如果这是一个问题,我会使用原子龙。

如果您查看源, 将会得到正确的答案, 即命令保证的“ 强” YES < / 强” 和“ 强” 。 < 强” 取决于使用哪种方法。

以下来源表明,它支持两种模式,视采用哪种方法而定。

  138       /**
  139        * Atomically sets the value to the given updated value
  140        * if the current value {@code ==} the expected value.
  141        *
  142        * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
  143        * and does not provide ordering guarantees, so is only rarely an
  144        * appropriate alternative to {@code compareAndSet}.
  145        *
  146        * @param expect the expected value
  147        * @param update the new value
  148        * @return true if successful.
  149        */
  150       public final boolean weakCompareAndSet(int expect, int update) {
  151           return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
  152       }

当有疑问时,读一下JavaDoc,如果还不清楚,读一下来源。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签