Java Review: Runnable Interface

Runnable Interface

              The runnable interface is a functional interface, meaning that it can be used with a lambda expression or method reference. A class can implement runnable and override the run() method, and perform an action when active.

              Threads run classes that implement runnable, making them active. If a task (another word for a runnable thread) needs to use thread methods, like getState() or interrupt(), it should be a class that implements thread, and not only runnable as the thread interface also implements the runnable interface.

              Advantages: Runnable lets a class be active while not implementing thread. You can create a runnable instance and pass it your thread.

              Disadvantages: Calling run() directly on a runnable will not start a new thread. The threads are required to run them to achieve new threads, and the process of feeding a runnable to a thread calls the runnable run() method.

Previous
Previous

Java Review: StringBuilder & StringBuffer

Next
Next

Java Review: Regular Expressions, Comparators, and Collections.