To implement a Runnable, one has only to implement the run method. One can create a thread on any object by implementing Runnable. It is the easiest way to create a thread by implementing Runnable. This interface is present in java.lang package. Also in multi-threaded programming, Runnable class is used. Runnable class is extensively used in network programming as each thread represents a separate flow of control. This becomes important as classes should not be subclassed unless there is an intention of modifying or enhancing the fundamental behavior of the class. When a thread is started by the object of any class which is implementing Runnable, then it invokes the run method in the separately executing thread.Ī class that implements Runnable runs on a different thread without subclassing Thread as it instantiates a Thread instance and passes itself in as the target. The most common use case of the Runnable interface is when we want only to override the run method. The runnable interface provides a standard set of rules for the instances of classes which wish to execute code when they are active. When the object of a class implementing Runnable class is used to create a thread, then the run method is invoked in the thread which executes separately. The method summary of the run() method is given below- Method The runnable interface has an undefined method run() with void as return type, and it takes in no arguments. It is an interface which is implemented by any class if we want that the instances of that class should be executed by a thread. Java runnable is an interface used to execute code on a concurrent thread.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |