如何使 Runnable 更改给定值 [英] How to make a Runnable Change a given value

查看:31
本文介绍了如何使 Runnable 更改给定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前面临的问题是我想编写一个方法,它创建一个可更改给定值的可运行对象;在这种情况下,它是一个布尔对象.

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.

然而:

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;
}

显然这不起作用,因为我不能直接为最终变量赋值,并且布尔对象没有设置"方法.但是,我需要将其声明为 final 才能创建 Runnable.

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.

那么有没有办法用可运行的对象来更改传递的值?(如果我能继续使用标准的 java 类型而不是发明"一些新类,那就太好了)

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)

如果是这样,是否有其他方法可以保存和传递方法?

If so, are there any alternatives for saving and passing methods?

任何帮助将不胜感激 (:

Any help would be appreciated (:

推荐答案

你调用的局部变量 Reference 只在 createOnOffSwitchRunnable 运行时存在,返回时消失;修改它没有意义.不过,如果在您的情况下有意义,您可以修改实例变量.如果您这样做,您应该声明变量 volatile 以防止过时读取(也称为 盲点定律").

The local variable you call Reference exists only while createOnOffSwitchRunnable is running and disappears when it returns; 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 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;
}

另一种选择是使 Reference 成为 Runnable 中的一个实例变量.这也可能解决您的问题,我不知道您要做什么.

Another option is making Reference an instance variable in the 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;
}

(另外,请使用标准命名约定,标题大小写Reference看起来像一个类的名称.)

(Also, please use standard naming conventions, written it title case Reference looks like the name of a class.)

这篇关于如何使 Runnable 更改给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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