Reputation: 1940
Is it a bad practice to put a Thread member inside a Runnable class like this:
public class A implements Runnable{
public Thread thread;
public A(){
thread = new Thread(this)
}
public void run(){
...
}
}
And invoke this Runnable class like that:
A a = new A();
a.thread.start();
...
a.thread.join();
...
Upvotes: 0
Views: 661
Reputation: 14259
Yes, because there is no (architectural) guarantee, that a Runnable always has the same thread. If you put it into an executor, your code will produce several errors. While your code works in your special case, you destroy the separation between thread and Runnable, and that one was created for a good reason.
If you need to access the actual thread for any reason, it is much better to use Thread.currentThread()
.
Upvotes: 1