What is the reason behind “get” method implementation of AtomicMarkableReference in java?

后端 未结 1 1833
猫巷女王i
猫巷女王i 2021-01-15 05:39

In java an AtomicMarkableReference can be used to update atomically an object reference along with a mark bit.

The javadoc states:

Im

相关标签:
1条回答
  • 2021-01-15 06:10

    This is because Java has no Pair<L, R> class and probably will not, even despite of the fact that standard library has at least three classes which have private static class Pair. Adding Pair class were discussed by OpenJDK developers more than once and proposal was always rejected. This mail is a very good explanation why pair shouldn't be presented as standard class (also, the whole mail thread is very useful):

    The problem is that classes like Pair simply go that much further to indulge the desire to never have to create any actual types of our own. When we're forced to create our own types, we begin to model our data more appropriately, which I believe leads us to create good abstractions at broader levels of granularity as well.

    As long as AtomicMarkableReference doesn't expose its Pair class and in Java you can't change value of passed reference (in the way that such change will be observable by caller), the only way to return both reference and bit flag is to return one of them from method and set second into passed as argument array. So it's not about concurrency, neither about legacy, it's about language design decision.

    0 讨论(0)
提交回复
热议问题