Multithreading in Java

Multithreading in Java allows a concurrent execution of two or more parts of the programme for maxim utilize  in the  CPU time.  Multithreading is actively used in animation and video game production. It has pros and cons as any other process. Usually more pros than the opposite. However, before moving to multithreading, just to remind you what thread means on the example of such object-oriented programming language as Java.

Each part of the programme is called a thread, the thread is a light-weighted subprocess which operates independently from others because it has separate path of execution.

There are few possible states of Thread:
NEW – t has not yet started is in this state.
RUNNABLE –  executing in the Java virtual machine is in this state.
BLOCKED –  blocked waiting for a monitor lock is in this state.
WAITING –  waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING –  is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED – has exited is in this state.
A thread can be in only one state at a given point in time.

Threads are created  in two ways :

  • Extending Thread class :

Starts with creating a class, which extends java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins inside run() method. Object , created in a new class and by calling the start() method starts the execution of a thread. Start() invokes the run() method on the Thread object

Example:

class Multithreading extends Thread{  
  public void run(){  
    System.out.println("My thread is in running state.");  
  }   
  public static void main(String args[]){  
     MultithreadingDemo obj=new MultithreadingDemo();   
     obj.start();  
  }  
}

Output:

My thread is in running state.
  • A new class implements java.lang.Runnable interface and override run()method. Then a Thread object is instantiated and start () method on this object is called.

Example:

class MultithreadingDemo implements Runnable{  
  public void run(){  
    System.out.println("My thread is in running state.");  
  }   
  public static void main(String args[]){  
     MultithreadingDemo obj=new MultithreadingDemo();  
     Thread tobj =new Thread(obj);  
     tobj.start();  
 }  
}

Output:

My thread is in running state.

Advantages of using multithreading

  • As was mentioned before the execution of multiple threads is used first of all to achieve multitasking.
  • Multiple threads are using a shared memory area, so in such case, multithreading wins over multiprocessing.
  • Context switching between threads takes less time
  • As all threads are independent they do not block a user in case if exception appears in one, it does not affect others
  • Allowing to perform multiple operations at the same time

 

Don't miss out!
Subscribe To Our Newsletter

Learn new things. Get an article everyday.

Invalid email address
Give it a try. You can unsubscribe at any time.

Comments

comments