Creating threads by extending the thread class

  • Eclipse: Oxygen
  • Java: 1.8

This tutorial offers a basic introduction to multithreading. This tutorial shows the basic execution of the program with multiple threads. The threads that have several unique properties, such as parallel execution, are shown here.

In the following program, we Extend “Thread class”. Then override the public void run() method which will be executed when the thread is started.

In for loop, we write the logic (looping from 1 to 9 to display number from 1 to 9). To start the thread, we create an object of the above class.

Call the start()method on the object created.  Now thread will start its execution in parallel.

We can call the run() method instead of the start () method, but it would behave like a normal method. This method should be called by JVM. It specified that if we call run() method of these threads directly then the execution of all of them is being handled by the same current thread and no multithreading will take place.

Output

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: 4 and the thread number is: 1

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

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

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

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

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

**************************

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

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: 2

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

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: 8 and the thread number is: 2

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

Total time required to process: 9000

In the following program, multithreading takes place and the output reflects simultaneous execution of threads.

When the execution method is called through the start () method, a new independent thread is assigned to execute the execution method, so if more than one thread calls the start method () it means that its execution method is run through separate threads.

Output

**************************

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

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

The value if i is: 2 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: 1

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: 1

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

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

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

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

Total time required to process: 4506

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

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