爪哇 - 偷位来自引用 [英] java - stealing bits from references

查看:131
本文介绍了爪哇 - 偷位来自引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么偷2个MSB从地址做一个原子操作?我试图做一个字CAS

How do I steal 2 MSBs from an address to do an atomic operation? I'm trying to do a single word CAS

一个例子

public class Node
{
    long key;
    long value;
    Node lchild; // format is flag1,flag2,address
    Node rchild; // format is flag1,flag2,address
}

public void createNode()
{
    Node n1 = new Node(); //this should create a node with format 0,0,address1
}

public void setFlag1(Node n1)
{
    Now the new address should be in format 1,0,address1
}

public void setFlag2(Node n1)
{
    Now the new address should be in format 0,1,address1
}

的AtomicReference 如果我只需要一个额外的标志可以使用。
AtomicStampedReference 可以使用,但因为它创建了一个包含时间戳和参考额外的盒子它是没有效率。

AtomicReference could be used if I needed only one extra flag. AtomicStampedReference could be used but it is not efficient as it creates an extra box containing timeStamp and a reference.

在C A类似的问题中讨论
挪用比特从一个指针

A similar problem in C is discussed in stealing bits from a pointer

推荐答案

这是不可能没有实现自己的JVM其正确支持这种操作和交易的标志位,例如执行GC时(所有引用需要在这一点上被识别和移动收藏家将需要改变它们)。即便如此,这是违反了Java的设计原则不包括间接引用明确或指针运算(我会数位改变在参考和掩蔽他们向非关联)。

This is impossible without implementing your own JVM which supports this kind of operations and deals with the flag bits properly, e.g. when doing GC (all references need to be identified at this point and moving collectors will need to change them). Even then, this is against the design principles of Java which do not include explicit dereferencing or pointer arithmetic (which I would count changing bits in a reference and masking them for dereferencing towards).

相反,我会建议你创建标志的新的复合型边和节点:

Instead, I would recommend you to create a new composite Edge type of the flags and the Node:

public class Node {
    long key;
    long value;
    Child lchild; // child = node reference + flags
    Child rchild;
}

// this is the edge type which should also be used to reference the root node with flags
public class Child {
    Node child;
    BitSet flags;

    public Child(Node node) {
        this.child = node;
        this.flags = new BitSet(2); // we initialize the flags with 00
    }
    public void setFlag(int index) {
        this.flags.set(index); // index would be 0 or 1 here for the two flags
    }
    public boolean isFlagSet(int index) {
        return this.flags.get(index); // index would be 0 or 1 here for the two flags
    }
}

这篇关于爪哇 - 偷位来自引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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