Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the java.lang.Thread class in Java not marked final by the designers?

What is the essence of allowing the user to create thread by extending the Thread class when we can achieve the same functionality by implementing Runnable and pass it to the Thread constructor.

like image 266
Sameer Naik Avatar asked Mar 26 '11 22:03

Sameer Naik


People also ask

Why isn't thread class Final Why should one extend thread?

One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. The other way to create a thread is to declare a class that implements the Runnable interface. So the answer is "you may want to subclass Thread to override its run() method."

What is Java Lang thread in Java?

The java.lang.Thread class is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.Following are the important points about Thread − Every thread has a priority.

What happens to a thread when it finishes Java?

So when does a thread finish? It happens in one of two cases: all instructions in the Runnable are executed. an uncaught exception is thrown from the run method.

Which method is not available in thread class?

Only start() and run() are defined by the Thread class. (2) and (3) are incorrect because they are methods of the Object class.


1 Answers

achieve the same functionality by implementing Runnable and pass it to the Thread constructor

The use of extending Thread is not limited to Runnable. For example you can change the behavior of some methods or add your own thread local information (always accessible with Thread.currentThread()).

like image 110
josefx Avatar answered Oct 06 '22 21:10

josefx