Runnable Object

suggest change

The Runnable interface defines a single method, run(), meant to contain the code executed in the thread.

The Runnable object is passed to the Thread constructor. And Thread’s start() method is called.

Example

public class HelloRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("Hello from a thread");
    }

    public static void main(String[] args) {
        new Thread(new HelloRunnable()).start();
    }
}

Example in Java8:

public static void main(String[] args) {
    Runnable r = () -> System.out.println("Hello world");
    new Thread(r).start();
}

Runnable vs Thread subclass

A Runnable object employment is more general, because the Runnable object can subclass a class other than Thread.

Thread subclassing is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread.

A Runnable object is applicable to the high-level thread management APIs.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Concurrent Programming Threads:
* Runnable Object

Table Of Contents
8 Arrays
10 Maps
11 Strings
16 Concurrent Programming Threads
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap