Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

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

Also, how does it work for anonymous classes?

1 Answer

0 votes
by (46k points)

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() {

   }

}

and execute it thus:

Runnable r = new MyRunnable(param_value);

new Thread(r).start();

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...