简体   繁体   English

Runnable可以返回一个值吗?

[英]Can a Runnable return a value?

Is it possible for a Runnable to return a value? Runnable是否可以返回值? I need to do some intensive work on an Editable and then return it back. 我需要对Editable进行一些密集的工作,然后将其返回。 Here is my mock code. 这是我的模拟代码。

public class myEditText extends EditText {
...
private Editable workOnEditable() {
    final Editable finalEdit = getText();
    Thread mThread = new Thread(new Runnable() {
        public void run() {


              //do work

              //Set Spannables to finalEdit

        }
     });
    mThread.start();
    return finalEdit;
}
...
}

So obviously my first problem is I'm trying to change finalEdit, but it has to be final in order to access it in and out of the thread, right? 所以显然我的第一个问题是我正在尝试更改finalEdit,但它必须是最终的才能进入和退出线程,对吧? What's the correct way to do this? 这样做的正确方法是什么?

In Java, a Runnable cannot "return" a value. 在Java中,Runnable不能“返回”一个值。

In Android specifically, the best way to handle your type of scenario is with AsyncTask . 在Android中,处理您的场景类型的最佳方法是使用AsyncTask It's a generic class so you can specify the type you want to pass in and the type that is returned to the onPostExecute function. 它是一个泛型类,因此您可以指定要传入的类型以及返回到onPostExecute函数的类型。

In your case, you would create an AsyncTask<Editable, Void, TypeToReturn> . 在您的情况下,您将创建一个AsyncTask<Editable, Void, TypeToReturn> Something like: 就像是:

private class YourAsyncTask extends AsyncTask<Editable, Void, Integer> {
     protected Long doInBackground(Editable... params) {
         Editable editable = params[0];
         // do stuff with Editable
         return theResult;
     }

     protected void onPostExecute(Integer result) {
         // here you have the result
     }
 }

The following exists simply to point out the use of "pseudo-closures" in Java, as unsuitable as they may be in this case. 以下仅仅是为了指出在Java中使用“伪闭包”,因为它们在这种情况下可能不合适


Java allows pseudo-closures through mutable-types stored in final variables. Java允许通过存储在最终变量中的可变类型进行伪闭包。

See Java's pseudo-closures by Christopher Martin skip to the "Faking it" section. 请参阅Christopher Martin的Java伪闭包,跳到“伪造它”部分。 He introduces a mutable-type ValueHolder<T> : 他介绍了一个可变类型的ValueHolder<T>

private static class ValueHolder<T> {
  T value;
}
...
final ValueHolder<Integer> i = new ValueHolder<Integer>();
...
i.value = 42;

Happy coding. 快乐的编码。

You realize that the thread keeps working past the end of the workOnEditable() function, right? 你意识到线程一直在workOnEditable()函数结束时工作,对吧? If you want a synchronous response, get rid of the thread. 如果你想要一个同步响应,摆脱线程。 If not, use a Handler to pass the data back to the main thread. 如果没有,请使用Handler将数据传递回主线程。

I have made 2 utility methods around Runnable that enable you to post a runnable and block until you get the result. 我在Runnable周围制作了两个实用工具方法,可以让你发布一个runnable和block,直到你得到结果。

You can take a look here: https://github.com/Petrakeas/HVTHandler/ 你可以看看这里: https//github.com/Petrakeas/HVTHandler/

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

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