Is there a way to pass parameters to a Runnable?

79,043

Solution 1

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

  @override
  public void run() {
    ...
  }
}

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

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

Solution 2

There are various ways to do it but the easiest is the following:

final int param1 = value1;
final int param2 = value2;
... new Runnable() {
    public void run() {
        // use param1 and param2 here
    }
}

Solution 3

If you need to communicate information into a Runnable, you can always have the Runnable object constructor take this information in, or could have other methods on the Runnable that allow it to gain this information, or (if the Runnable is an anonymous inner class) could declare the appropriate values final so that the Runnable can access them.

Hope this helps!

Solution 4

Although you can use any of the above the answer, but if you question is really concerned about android then you can also use AsyncTask.

Share:
79,043
Yevgeny Simkin
Author by

Yevgeny Simkin

I fled Soviet Russia as a child and have spent my life bouncing from music to comedy to software engineering. You can follow my comedy Twitter feed here. I'm also the founder and CEO of The Russian Mob™—an agency specializing in developing SAAS, Mobile, AR, VR, and Web applications. (No: we won’t help you hack a foreign election so don’t bother asking.) On occasion, things that I think are published over at The Bulwark, which you should be reading, even if my ideas weren't being published there. If you would like to connect with me, the easiest way is through LinkedIn.

Updated on July 05, 2020

Comments

  • Yevgeny Simkin
    Yevgeny Simkin almost 4 years

    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.

    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.

  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    Will Callable be ok with being run from a thread that isn't the UI thread? The reason I'm doing this to begin with is that you can't just call out to the main thread in Android if you're going to alter any UI elements.
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    I guess I should have specified that I'd like to avoid using global params in my class in this way... :) Thanks, but I'm really trying to get a method for passing arguments in or using some other construct that takes them (if Runnable doesn't)
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    I like this approach, but how do I communicate with this class from inside my timer thread (the Runnable is out in the main UI thread). Can I just make the member public and set it in my timer thread prior to passing the Runnable to the handler? Seems too good to be true :)
  • templatetypedef
    templatetypedef over 12 years
    @Dr.Dredel- Yes, any thread should be able to run a Callable.
  • Paul Bellora
    Paul Bellora over 12 years
    +1 for using an anonymous class extending Runnable and referencing final variables, as long as it's only used in one spot
  • Lalit Poptani
    Lalit Poptani over 12 years
    Sorry to reply late, edited my answer.
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    For some reason I thought that if obj is created some place other than the UI thread, then when it tries to manipulate a View (in the main thread) the app will crash. I'll give it a whirl, thanks very much.
  • Yevgeny Simkin
    Yevgeny Simkin over 12 years
    AsyncTask can manipulate Views in the UI thread (I realize this is only relevant in Android)?
  • Lalit Poptani
    Lalit Poptani over 12 years
    If you would like to manipulate or invalidate a view, you can use runOnUiThread() or can use Handler to ivalidate the view on the UI thread.
  • Romain Guy
    Romain Guy over 12 years
    You can store them as fields in the Runnable but that's about it. You could also use a Callable.
  • assylias
    assylias almost 11 years
    Except that MyRunnable does not extend Runnable so you won't be able to use it where a Runnable is expected.
  • ilomambo
    ilomambo almost 11 years
    @assylias An interface can extend runnable if needed.
  • assylias
    assylias almost 11 years
    Yes but that is not the issue. Typically you call new Thread(new MyRunnable() {...});, but that will call the run() method, not the run(int data); method. Unless you have the run method call the run(int data) method, but how do you pass the parameters then? Try using your proposal with a real example and you will see the problems.
  • ilomambo
    ilomambo almost 11 years
    @assylias I am using my proposal, but with methods I wrote, so I can call run(data). I guess you are correct about the OS methods, which will call run() only.
  • marcelnijman
    marcelnijman over 9 years
    That is going to run into a synchronization problem when the second post is called before the first post has been handled by the Runnable.
  • Lo-Tan
    Lo-Tan over 9 years
    @ilomambo The accepted answer is the proper way to do this. Due to the nature of the asker's question (passing a Runnable with arguments to be executed in another thread), this answer does not work at all.
  • John Sums
    John Sums over 9 years
    -1 for saying Callable accepts parameters. A Callable returns a result, but the call method signature doesn't accept parameters.
  • templatetypedef
    templatetypedef over 9 years
    @JohnSums You're right! Let me go fix that.
  • Oliver Hausler
    Oliver Hausler almost 8 years
    run() should be marked with @Override. I edited the answer.
  • Daniel Sharp
    Daniel Sharp over 5 years
    There's Consumer now for single parameters.