Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread class empty constructor

I was wondering what's the reasoning of the existance of an empty constructor on Thread class.

Since you cant give it a Runnable when it's created, creating a Thread like this:

Thread t=new Thread();

Is completely useless.

Can you think of a reason why there is not an option of adding a runnable to a thread AFTER CREATION?

like image 883
Ofek Ron Avatar asked Feb 11 '12 21:02

Ofek Ron


3 Answers

You can override the Thread class, too. Your own implementation could then do something sensible in the run() method without the need for a Runnable.

like image 182
Bombe Avatar answered Sep 30 '22 10:09

Bombe


The following works:

new Thread() {
    public void run() {
         System.out.println("Well you can change the run method.");
    }

}

but yes that's not what I'd consider good practice.

like image 39
Voo Avatar answered Sep 30 '22 08:09

Voo


Thread class can be subclassed, and it's run() overriden. See the Javadoc.

like image 44
david a. Avatar answered Sep 30 '22 10:09

david a.