Multithreading

Multithreading is a widespread programming and execution model that allows multiple threads to exist within the context of one process. These threads share the common resources of a single process and are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. Multithreading is widely used in processes which require one process to enable parallel execution on a multiprocessing system.

Where a thread of execution is a smallest sequence of programmed instruction that can be managed independently, typically the part of platform on which they are running either on JVM or on operating system itself.

Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application.

For example, the transmission rate of data over a network is much slower than the rate at which the computer can process it. Even local file system resources are read and written at a much slower pace than they can be processed by the CPU. And, of course, user input is much slower than the computer. In a single-threaded environment, your program has to wait for each of these tasks to finish before it can proceed to the next one—even though most of the time the program is idle, waiting for input. Multithreading helps you reduce this idle time because another thread can run when one is waiting.

Multithreading in java can be achieved via two ways:

  1. Via extending Thread class:
class MultithreadingDemo extends Thread{

//run method of Thread class is overridden here

    public void run(){

        System.out.println("hi, I am running");

    }

    public static void main(String[] args){

       MultithreadingDemo multiThreadingObj = new MultithreadingDemo();

       //start method of Thread class starts the current tread

       multiThreadingObj.start();

    }

}
//##output :  hi, I am running

Thread class provides constructor and method to create and perform operation on a Thread.

2  Via implementing Runnable interface:

class MultithreadingDemo implements Runnable{

//implement run method of Runnable interface

    public void run(){

        System.out.println("hi, I am running");

    }

    public static void main(String[] args){

        MultithreadingDemo multiThreadingObj = new MultithreadingDemo();

        //create object of Thread class

        Thread threadObj = new Thread(multiThreadingObj);

        threadObj.start();

    }

}
//##output :  hi, I am running

Class implementing Runnable interface, need to provide definition of run method of Runnable interface.

Note: when start method of newly created thread is called then it enters into runnable states.

Merits of Multithreading

  1. Threads work independently and if any exception occurs it affects only that thread.
  2. Threads are light weight.
  3. Cost of communication is low between treads.
  4. It saves time of achieving multiple tasks done.

Demerits of Multithreading

  1. It is complex to debug and test the program while using Multithreading.
  2. Chances of deadlock to occur, increases.
  3. Need to replace those functions which are not thread safe.
  4. Need to remove static variables.
  5. Not easy to port existing code into Multithreading.

Leave a Reply

Your email address will not be published. Required fields are marked *