Thread class empty constructor

送分小仙女□ 提交于 2021-02-19 00:38:47

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/9244147/thread-class-empty-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!