Back
Can anyone suggest to me how I can pass a parameter to a thread?
Also, how does it work for anonymous classes?
You require to relinquish the parameter in the constructor to the Runnable object:
public class MyRunnable implements Runnable { public MyRunnable(Object parameter) { // store parameter for later user } public void run() { }}
public class MyRunnable implements Runnable {
public MyRunnable(Object parameter) {
// store parameter for later user
}
public void run() {
and execute it thus:
Runnable r = new MyRunnable(param_value);new Thread(r).start();
Runnable r = new MyRunnable(param_value);
new Thread(r).start();
31k questions
32.8k answers
501 comments
693 users