Java Threads - IT magazine

IT magazine

Knowledge that matters

Java Threads

Share This

Introduction to Threads

               Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.

Multithreading has several advantages over Multiprocessing such as
  • Threads are lightweight compared to processes
  • Threads share the same address space and therefore can share both data and code
  • Context switching between threads is usually less expensive than between processes
  • Cost of thread intercommunication is relatively low that that of process intercommunication
  • Threads allow different tasks to be performed concurrently

Thread Creation

There are two ways to create thread in java
  1. Implement the Runnable interface (java.lang.Runnable)
  2. By Extending the Thread class (java.lang.Thread)
The procedure for creating threads based on the Runnable interface is as follows:
  • A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable Object
  • An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.
  • The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned 
  • The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.

The procedure for creating threads based on extending the Thread is as follows:
  • A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.
  • This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.
  • The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.

2 comments:

  1. I would like to add something I know...

    If the class which we are going to extend from Thread class is not extended previously by any other class, we can easily extend it from Thread class.

    If it is already extended by some other class, it will be impossible to extend from Thread class. Because, "A class must have only one parent class".,i.e.,Multiple Inheritance is not allowed in Java. That's why we have to implement Runnable interface which contains only run() method and then redefine run().

    Then I want some clarity, what is the purpose of passing the Runnable object as an argument for Thread class?

    ReplyDelete
    Replies
    1. This is because a class implemented by Runnable interface can not use all the thread class methods. In order to use thread methods the Runnable object is passed to Thread constructor which then creates an Thread class object which can use all the methods of Thread and when ever run() is invoked it is redirected to Runnanble interface run()

      Delete