如何将变量传递给新的Runnable声明? [重复]

时间:2022-01-08 17:31:33

This question already has an answer here:

这个问题在这里已有答案:

I have the following :

我有以下内容:

Runnable done = new Runnable()
    {
        public void run()
        {
            System.out.println("Hello");
        }
    };

And then in my Android activity I'll call something like :

然后在我的Android活动中,我会称之为:

runOnUIThread(done);

Which I then call. However, I want that "Hello" not to be hardcoded, so I can pass it in. Otherwise I'll have to have one of these declarations for every String I want to print.

然后我打电话给你。但是,我希望“Hello”不要被硬编码,所以我可以传入它。否则我必须为我想要打印的每个String都有一个这样的声明。

(This is actually an android question, but slimmed it down to basic Java so its easier to answer)

(这实际上是一个android问题,但是它缩小到基本的Java,因此更容易回答)

Thanks

2 个解决方案

#1


22  

final String hello = whereverItsComingFrom;
Runnable done = new Runnable()
    {
        public void run()
        {
            System.out.println(hello);
        }
    };

#2


47  

In Java (and I believe it is the same in Android too), you can use a anonymous inner class, like suggested by Bart van Heukelom. This solution has the advantage that have to write less code, and you could access fields and methods ouf the outer class.

在Java中(我相信它在Android中也是如此),你可以使用一个匿名的内部类,就像Bart van Heukelom所建议的那样。此解决方案的优点是必须编写更少的代码,并且可以访问外部类的字段和方法。

But it has 2 drawbacks:

但它有两个缺点:

  • the variable "hello" has to be final,

    变量“hello”必须是final,

  • the anonymous class has a internal reference to the outer class instance - this means the outer class being retained when it would otherwise be eligible for garbage collection. @See: Effective Java [Joshua Bloch], Item 22: Favor static member classes over nonstatic

    匿名类具有对外部类实例的内部引用 - 这意味着外部类在有资格进行垃圾回收时保留。 @See:Effective Java [Joshua Bloch],Item 22:赞成非静态的静态成员类

And in my humble opinion, it is bad practice to parametrize a class instance in this way.

在我看来,以这种方式参数化类实例是不好的做法。

So I believe, as long as you do not need to access methods and fields of the outer class, it is better to write an specific class for this task and make it a static member class.

所以我相信,只要您不需要访问外部类的方法和字段,最好为此任务编写特定的类并使其成为静态成员类。

class Demo {
...

  private static class MyRunnable implements Runnable {
     private final String message;

     MyRunnable(final String message) {
       this.message = message;
     }

     public void run() {
       System.out.println(message);
     }
  }

  public void startThread() {
    MyRunnable myRunnable = new MyRunnable("Hello");

    runOnUIThread(myRunnable); 
  }
...
}

#1


22  

final String hello = whereverItsComingFrom;
Runnable done = new Runnable()
    {
        public void run()
        {
            System.out.println(hello);
        }
    };

#2


47  

In Java (and I believe it is the same in Android too), you can use a anonymous inner class, like suggested by Bart van Heukelom. This solution has the advantage that have to write less code, and you could access fields and methods ouf the outer class.

在Java中(我相信它在Android中也是如此),你可以使用一个匿名的内部类,就像Bart van Heukelom所建议的那样。此解决方案的优点是必须编写更少的代码,并且可以访问外部类的字段和方法。

But it has 2 drawbacks:

但它有两个缺点:

  • the variable "hello" has to be final,

    变量“hello”必须是final,

  • the anonymous class has a internal reference to the outer class instance - this means the outer class being retained when it would otherwise be eligible for garbage collection. @See: Effective Java [Joshua Bloch], Item 22: Favor static member classes over nonstatic

    匿名类具有对外部类实例的内部引用 - 这意味着外部类在有资格进行垃圾回收时保留。 @See:Effective Java [Joshua Bloch],Item 22:赞成非静态的静态成员类

And in my humble opinion, it is bad practice to parametrize a class instance in this way.

在我看来,以这种方式参数化类实例是不好的做法。

So I believe, as long as you do not need to access methods and fields of the outer class, it is better to write an specific class for this task and make it a static member class.

所以我相信,只要您不需要访问外部类的方法和字段,最好为此任务编写特定的类并使其成为静态成员类。

class Demo {
...

  private static class MyRunnable implements Runnable {
     private final String message;

     MyRunnable(final String message) {
       this.message = message;
     }

     public void run() {
       System.out.println(message);
     }
  }

  public void startThread() {
    MyRunnable myRunnable = new MyRunnable("Hello");

    runOnUIThread(myRunnable); 
  }
...
}