Skip to content
Advertisement

Creating threads without extends and implements

I was going through an old project (Java) in my company where I found the author (already left), creates and runs thread without extending the Thread class or implementing the Runnable interface. One noticable thing was that the class was a singleton class. There are no usage of thread pooling, or the new concurrent package from java. Code snippet outlines given below –

JavaScript

How does this approach work – using threads without extending from Thread class or implementing Runnable interface? What are the advantages and disadvantages of using this approach (without extends and implements).

Advertisement

Answer

JavaScript

This code creates an anonymous class that extends Thread.

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

The above quote is taken from The Java Tutorials, where you can read more about them.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement