Can anyone suggest to me how I can pass a parameter to a thread?
Also, how does it work for anonymous classes?
|
Can anyone suggest to me how I can pass a parameter to a thread? Also, how does it work for anonymous classes? |
|||||||||||||
|
|
You need to pass the parameter in the constructor to the thread object:
and invoke it thus:
|
|||||||||||||||||||||
|
For Anonymous classes:In response to question edits here is how it works for Anonymous classes
Named classes:You have a class that extends Thread (or implements Runnable) and a constructor with the parameters you'd like to pass. Then, when you create the new thread, you have to pass in the arguments, and then start the thread, something like this:
Runnable is a much better solution than Thread BTW. So I'd prefer:
This answer is basically the same as this similar question: How to pass parameters to a Thread object |
|||||||||
|
|
via constructor of a Runnable or Thread class
|
|||||
|
|
When you create a thread, you need an instance of
If you then want to change the parameter while the thread is running, you can simply add a setter method to your runnable class:
Once you have this, you can change the value of the parameter by calling like this:
Of course, if you want to trigger an action when the parameter is changed, you will have to use locks, which makes things considerably more complex. |
||||
|
|
|
To create a thread you normally create your own implementation of Runnable. Pass the parameters to the thread in the constructor of this class.
|
||||
|
|
|
Either write a class that implements Runnable, and pass whatever you need in a suitably defined constructor, or write a class that extends Thread with a suitably defined constructor that calls super() with appropriate parameters. |
|||
|
|
|
You can either extend the
|
|||
|
|
|
This answer comes very late, but maybe someone will find it useful. It is about how to pass a parameter(s) to a
Of course you can postpone execution of |
|||
|
|
|
You can derive a class from Runnable, and during the construction (say) pass the parameter in. Then launch it using Thread.start(Runnable r); If you mean whilst the thread is running, then simply hold a reference to your derived object in the calling thread, and call the appropriate setter methods (synchronising where appropriate) |
|||
|
|
|
Parameter passing via the start() and run() methods:
|
||||
|
|
|
There is a simple way of passing parameters into runnables. Code:
|
|||
|
|
|
One further option; this approach lets you use the Runnable item like an asynchronous function call. If your task does not need to return a result, e.g. it just performs some action you don't need to worry about how you pass back an "outcome". This pattern lets you reuse an item, where you need some kind of internal state. When not passing parameter(s) in the constructor care is needed to mediate the programs access to parameters. You may need more checks if your use-case involves different callers, etc.
} Thread t = new Thread(new MyRunnable(parameter)); t.start(); If you need a result of processing, you will also need to coordinate completion of MyRunnable when the sub-task finishes. You could pass a call back or just wait on the Thread 't', etc. |
|||
|
|
Specially for AndroidFor callback purposes I usually implement my own generic
Usage is simple:
In manager:
|
||||
|
|
|
No you can't pass parameters to the
Once you do that - you have to be careful of the data integrity of the object you pass into the 'WorkingTask'. The data will now exist in two different threads so you have to make sure it is Thread Safe. |
||||
|
|