简体   繁体   English

如何对可运行的值进行给定值更改

[英]How to make a Runnable Change a given value

So the problem I am currently facing is that I want to write a method, that creates a runnable that changes a given Value; 因此,我目前面临的问题是我想编写一个方法,该方法创建一个可运行的可更改给定值的对象; in this case it's a Boolean Object. 在这种情况下,它是一个布尔对象。

(I use this, to make it possible to react on different ways to a Key Press) (我用它来使对按键产生不同的反应)

If I am just using a Method of the passed Object it works just fine. 如果我只是使用传递的对象的方法,它就可以正常工作。

However: 然而:

public static Runnable createOnOffSwitchRunnable(Boolean b)
{
    final Boolean Reference = b;
    Runnable R = new Runnable()
    {
        public void run()
        {
            if (Reference.booleanValue() == true)
            {
                Reference = false;
            }
        }
    };
    return R;
}

Obviously this does not work, as I can't directly assign a value to the final Variable and the Boolean Object has no "set"-method. 显然,这是不行的,因为我不能直接赋值给最后一个变量和布尔对象没有“设置” -方法。 However I NEED to declare it final to even be able to create the Runnable. 但是我需要声明它最终才能创建Runnable。

So is there no way how you can change a passed value, with a runnable? 那么,有没有办法用可运行的方式更改传递的值呢? (it would be nice if I could keep on using standard java Types instead of "inventing" some new class) (如果我可以继续使用标准的java类型而不是“发明”一些新类,那将是很好的)

If so, are there any alternatives for saving and passing methods? 如果是这样,是否还有其他方法可以保存和传递方法?

Any help would be appreciated (: 任何帮助,将不胜感激 (:

You can use this trick, which is as close as you'll get to your intention: 您可以使用此技巧,它将尽您所能:

public static Runnable createOnOffSwitchRunnable(final Boolean... b) {
    return new Runnable() {
        public void run() {
            if (b[0].booleanValue()) {
                b[0] = false;
            }
        }
    };
}

I've done the following: 我已经完成以下工作:

  • made the parameter final, instead if creating a separate final local variable 将参数设置为final,而不是创建单独的final局部变量
  • made the parameter varags, which is actually an array inside the method, but which the caller can call with a single Boolean parameter so effectively the signature looks the same for this case 设置了参数varags,该参数实际上是方法内部的一个数组,但是调用方可以使用单个布尔参数进行调用,因此在这种情况下,签名看起来实际上是相同的
  • the parameter array is final, but its contents are not - this is the "trick" 参数数组是最终的,但其内容不是-这是“技巧”
  • simplified the logic:: testing booleanValue==true is an anti-pattern, just test booleanValue itself 简化了逻辑::测试booleanValue==true是一种反模式,只需测试booleanValue本身
  • return the Runnable without creating a (needless) local variable 返回Runnable而不创建(不必要的)局部变量

The local variable you call Reference exists only while createOnOffSwitchRunnable is running and disappears when it returns; 您调用的Reference的局部变量仅在createOnOffSwitchRunnable运行时存在,并在返回时消失。 it makes no sense to modify it. 修改它没有任何意义。 You can modify an instance variable though, if it makes sense in your case. 不过,您可以修改实例变量(如果您认为合适的话)。 If you do this you should declare the variable volatile to prevent stale reads (also called "the law of the blind spot" ). 如果这样做,则应声明变量volatile以防止过时的读取(也称为“盲区法则” )。

volatile Boolean Reference;

public static Runnable createOnOffSwitchRunnable(Boolean b)
{
    Reference = b;

    Runnable R = new Runnable()
    {
        public void run()
        {
            if (Reference.booleanValue() == true)
            {
                Reference = false;
            }
        }
    };
    return R;
}

Another option is making Reference an instance variable in the Runnable. 另一个选择是使Reference成为Runnable中的实例变量。 This might also solve your problem, I have no idea what you're trying to do. 这也可能会解决您的问题,我不知道您要做什么。

public static Runnable createOnOffSwitchRunnable(final Boolean b)
{

    Runnable R = new Runnable()
    {
        private Boolean Reference = b;

        public void run()
        {
            if (Reference.booleanValue() == true)
            {
                Reference = false;
            }
        }
    };
    return R;
}

(Also, please use standard naming conventions, written it title case Reference looks like the name of a class.) (此外,请使用标准的命名约定,将其写为标题大小写Reference看起来像一个类的名称。)

Use an AtomicBoolean . 使用AtomicBoolean It will have the advantage of being thread-safe. 它将具有线程安全的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM