Java - Runnable

> Procedural Languages > Java > Java - Concurrency (Parallel Work)

1 - About

A Runnable is a configuration object that define the code that will run inside threads. See example

Advertising

3 - Example

  • You define the code to be run in the method, run
  • You pass the Runnable object to the Thread constructor.
  • And you start the thread
public class HelloRunnable implements Runnable {
 
    public void run() {
        System.out.println("Hello from a thread!");
    }
 
    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }
 
}