4

Assume I would like to pass the following variable

String foo = "hello world";

as an argument to the following thread

new Thread(new Runnable() {

    @Override
    public void run() {
        // SOME CODE HERE REQUIRES VARIABLE
    }
}).start();

Can someone please explain how to do this.

Thanks.

androideka
  • 71
  • 1
  • 11
  • see here: http://stackoverflow.com/questions/877096/how-can-i-pass-a-parameter-to-a-java-thread – The Good Giant Mar 26 '13 at 16:13
  • @TheGoodGiant in the linked question Thread is subclassed while here it is not. Also here there is a simpler solution available... – vidstige Mar 26 '13 at 20:50

5 Answers5

4

Subclass Thread:

public class MyThread extends Thread {

   private String arg;

   public MyThread(String arg) {
      this.arg = arg;
   }

   @Override
   public void run() {
        // Use your variable
   }
}
Peter Bratton
  • 6,302
  • 6
  • 39
  • 61
3

Can someone please explain how to do this.

So your problem has more solutions but nicely from the beginning. I suggest you to create own subclass of Thread and pass parameter via its constructor.

public class MyThread extends Thread {

    public MyThread(String value) {

    }
}

Or you can also use Runnable interface as well.

public class MyThread implements Runnable { ... }

Update:

Or like @erickson pointed out you can wrap your code into body of method but as method's argument you have to pass final variable because you cannot cannot refer to a non-final variable inside inner class defined in a different method.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • 2
    -1 No need to belittle questioner. It's a reasonable question. – Peter Bratton Mar 26 '13 at 16:15
  • @jordan002 belittle? are you kidding me? i wrote three possible approaches he can use. I mentioned subclass because if he will want to change this variable since is declared as final this is not possible. Only define next helping variable. sorry but i disagree with you. – Simon Dorociak Mar 26 '13 at 16:26
  • If he does want to change the value of the variable from another thread, there are many pitfalls. – erickson Mar 26 '13 at 16:33
  • @erickson i know :) it's not trivial. – Simon Dorociak Mar 26 '13 at 16:38
  • @Sajmon Judging from your reaction I'm guessing you get this kind of feedback a lot. If the question was obvious, there'd be no reason to ask. Not to mention the fact that a linked question has (right now) about 42 upvotes. Seems a pretty good question to me. – Peter Bratton Mar 26 '13 at 16:41
  • 1
    @jordan002 no man, i was very surprised only from your feedback, it was first time, this kind of reaction and now i know what you hate, its words i used, truly sorry, i fix it. – Simon Dorociak Mar 26 '13 at 16:43
  • @jordan002 i updated answer, now everything is ok? :) – Simon Dorociak Mar 26 '13 at 16:45
  • 1
    @Sajmon Looks good. Your answer is spot on, FWIW. – Peter Bratton Mar 26 '13 at 16:46
3

Local variables that are declared as final will be visible in the thread:

void doSomething(final String foo) {
  new Thread(new Runnable() {
      @Override
      public void run() {
        // SOME CODE HERE REQUIRES VARIABLE
        System.out.println(foo);
      }
  }).start();
}
erickson
  • 265,237
  • 58
  • 395
  • 493
0

You can't pass argument using anonymous classes in Java. What you can do is create a separate class and pass it as instance variable

Jabir
  • 2,776
  • 1
  • 22
  • 31
0
String foo = "hello world";
final String parameter = foo;
new Thread(new Runnable() {

    @Override
    public void run() {
         //Use parameter
    }
}).start();
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33