i

JAVA Complete Course

Creating A Thread

In java there are two ways of creating actual thread class. One is implementing Runnable interface and override run method and the second is extending Thread class and override run method. This is the ways how we are creating our thread classes.  We will see both examples below but before it let’s shortly say how to run our thread.

For running thread, we should create new instance of thread class which in constructor takes a runnable as a parameter. On our instance we need to call start() method in order to reach multithreading, otherwise we will have still single threaded application.

Let’s see how to create thread with implementing Runnable interface and run it.

A screenshot of a cell phone

Description automatically generated

Now let’s see another example, creating and running thread by extending Thread class: