Creating threads by implementing Runnable interface

  • Eclipse: Oxygen
  • Java: 1.8

There are two ways for creating threads and associate them with the required code. One was already seen in the previous tutorial and the second is through the use of the Runnable Interface. The runnable interface declares the run method that must be implemented by the class (implementing it).

For creating a thread by implementing Runnable Interface

First, we create a class that implements the Runnable interface. Override the run () method that will be executed when this thread starts. Within the run () method we provide a logic.

To run this implementation class, create a Thread object, pass Runnable implementation class object to its constructor as a parameter.

A Thread object is created that can execute its Runnable class. Now we call the start () method on the allocated thread object.

Note: The implementation of the Runnable interface does not create a Thread object, it only defines an entry point for threads in its object. It allows passing the object to the Thread constructor.

In the following program, we have created two threads “Thread1” and “Thread2”. Both threads will show the numbers 1, 2…9 with a delay of half a second to show the next number. The App class will start these threads “Thread1” and “Thread2”.

Output

The value if i is: 0 and the thread number is: 2

The value if i is: 0 and the thread number is: 1

The value if i is: 1 and the thread number is: 2

The value if i is: 1 and the thread number is: 1

The value if i is: 2 and the thread number is: 1

The value if i is: 3 and the thread number is: 1

The value if i is: 2 and the thread number is: 2

The value if i is: 3 and the thread number is: 2

The value if i is: 4 and the thread number is: 1

The value if i is: 4 and the thread number is: 2

The value if i is: 5 and the thread number is: 1

The value if i is: 5 and the thread number is: 2

The value if i is: 6 and the thread number is: 1

The value if i is: 6 and the thread number is: 2

The value if i is: 7 and the thread number is: 2

The value if i is: 7 and the thread number is: 1

The value if i is: 8 and the thread number is: 2

The value if i is: 8 and the thread number is: 1

The value if i is: 9 and the thread number is: 2

The value if i is: 9 and the thread number is: 1

If you know that you would be making use of threads only once. Then you can make use of the anonymous object.

In the following program demonstrates that we can create an anonymous object and start the threads. This compressed code.

Here we create an object of an anonymous class, which has implemented the Runnable interface and its run() method.

Then we pass the object of this anonymous class to the constructor of the Thread since Thread constructor takes Runnable and the anonymous class has also become a type of Runnable interface. Then we are starting the anonymous thread.

Output

1

2

3

4

5

6

7

8

9

We can implement the Runnable interface and its run () method in this way.

In the following code, we create an anonymous object and start the thread immediately.

Output

1

2

3

4

5

6

7

8

9

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu