I understand that if you want to thread you can either extend thread or implement runnable to multithread in java. But why do you have to implement an interface for java to thread? Whats the importances of the runnable interface that makes java threading work? Does Java’s interface extend from something?
Share
The only thing special about the
Runnableinterface is that it is whatThreadtakes in its constructor. It’s just a plain-old interface.As with most interfaces, the point is that you’re programming to a contract: you agree to put the code you want to run in the
Runnable#run()implementation, andThreadagrees to run that code in another thread (when you create and start aThreadwith it).It’s
Threadthat actually “does” the multithreading (in that it interacts with the native system). An implementation ofRunnableis just where you put the code that you want to tell aThreadto run.In fact, you can implement a
Runnableand run it, without having it run in a separate thread:So
Runnableitself doesn’t have anything to do with multi-threading, it’s just a standard interface to extend when encapsulating a block of code in an object.