steve steve - 1 year ago 78
Java Question

How can I pass a parameter to a Java Thread?

Can anyone suggest to me how I can pass a parameter to a thread?

Also, how does it work for anonymous classes?

Answer Source

You need to pass the parameter in the constructor to the thread object:

public class MyThread implements Runnable {

   public MyThread(Object parameter) {
       // store parameter for later user

   public void run() {

and invoke it thus:

Runnable r = new MyThread(param_value);
new Thread(r).start();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download