Runnable or Thread
Java multithreading
Thread is a block of code which can be execute concurrently with other threads in the JVM. To implement Java thread is just to specify what code a thread should run.
extends Thread class
create a subclass of Thread and override the run() method:
you can also create an anonymous subclass of Thread like this:
implement interface java.lang.Runnable
To have the run() method executed by a thread, pass an instance of MyRunnable to a Thread in its constructor.
You can also create an anonymous implementation of Runnable, like this:
Subclass or Runnable?
The most common difference is
- extend Thread class, you can’t extend any other class which you required.
- implement Runnable interface, you can save a space for your class to extend any other class in future or now.
The significant difference is
If you like this post or if you use one of the Open Source projects I maintain, say hello by email. There is also my Amazon Wish List. Thank you ♥
Comments