有没有一种方法将参数传递给一个Runnable? [英] Is there a way to pass parameters to a Runnable?

查看:1716
本文介绍了有没有一种方法将参数传递给一个Runnable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一个处理器来发布可运行实例的线程。它工作得很好,但我很好奇,我怎么会通过PARAMS在Runnable的实例中使用?也许我只是不理解此功能如何工作的。

I have a thread that uses a handler to post a runnable instance. it works nicely but I'm curious as to how I would pass params in to be used in the Runnable instance? Maybe I'm just not understanding how this feature works.

要pre抢先一你为什么需要这个的问题,我有螺纹的动画,有回调出UI线程告诉它什么实际绘制。

To pre-empt a "why do you need this" question, I have a threaded animation that has to call back out to the UI thread to tell it what to actually draw.

推荐答案

只是一个类实现的Runnable 与构造函数接受的参数可以做的,

Simply a class that implements Runnable with constructor that accepts the parameter can do,

public class MyRunnable implements Runnable {
  private Data data;
  public MyRunnable(Data _data) {
    this.data = _data;
  }

  public void run() {
    ...
  }
}

您可以只创建了Runnable类参数的构造函数的一个实例。

You can just create an instance of the Runnable class with parameterized constructor.

MyRunnable obj = new MyRunnable(data);
handler.post(obj);

这篇关于有没有一种方法将参数传递给一个Runnable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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