Most of the time I want to run a block of code in a new thread where it does some time consuming stuff but in some circumstances I want to run it with the thread that called a method. Is it bad practice to directly call the run method?
If it is not a good idea is it best to put the code in another method which can be called by the new thread or duplicate the code?
This is some example code, it is just to illustrate what I want to achieve:
class Main {
public Main() {
new Thread(new WorkerThread(1, 2)).start(); // This is what I'd do if I wanted it to run in a new thread.
new WorkerThread(1, 2).run(); // Is it bad practice to do this?
}
}
class WorkerThread implements Runnable {
private int int1, int2;
public WorkerThread(int int1, int int2) {
this.int1 = int1;
this.int2 = int2;
}
public void run() {
// Do something time consuming with the ints.
}
}