Java Multithreading

This article gives an overview of Java multithreading. It talks of the advantages of multithreading, thread states, priorities and options to create threads in Java.

Java supports development of multithreaded programs. In a program where two or more tasks can be executed concurrently is called a multi threaded program. In such a program, each task is responsible for its own execution of instructions where underplaying recourses are optimally used especially when hardware is multi core.

In multi tasking different processes or threads share common resources like memory and CPU. Multi threading is a specialized case of Multi tasking where an application is divided into different components that are executed concurrently. These threads are executed in parallel. The time is divided between each threads based on different algorithms. It is a division of time slots between applications and then between threads. Operating system is responsible to manage the time slots. Multi threading helps us to write applications where different tasks are performed concurrently.

Advantages of Multi threading

  • Threads are independently executed in a multi threaded java program without operating operations of other users at the same time.
  • Multi threading allows you to do different tasks at the same time. It saves time.
  • Threads are independent and do not affect other threads. For example an exception in a thread will not affect other running threads.

Thread Life Cycle

During execution of an application, a thread goes through multiple stages. Following are the stages of thread:

  1. Born
  2. Started
  3. Runs
  4. Dies

In the diagram below, these stages are explained.
thread-states

 

  • New: When a thread is created, it starts its cycle with a new state. It is remained in this state until it is explicitly started. Sometime it is also called a new born thread state.
  • Runnable: When a newly created thread is started, it becomes runnable. A thread in a runnable state is considered of executing the tasks.
  • Waiting: There is a situation when a thread needs to wait for another thread to complete it execution. In such a case, thread is in wait state. It again becomes runnable when the other thread completes its task and signals the waiting thread.
  • Timed Waiting: For a specified time period, a runnable thread can enter into wait state. After the completion of wait period, these waitings thread transition back to runnable state.
  • Terminated: When a task is completed by a running thread or it is interrupted, it enters the terminated state.

Thread Priorities

Java threads are scheduled by the operating system based on the priority assigned to it. An execution priority is assigned to each thread. Following is the range of java thread priority.

Default value of every java thread is NORM_PRIORITY (a constant value of 5). Higher priority threads are important ones and allocated time before low priority threads. On the other side, these thread priorities do not guarantee execution order and are platform dependent.

Creating threads

There are two ways to create a thread.

  • Implementing an interface
  • Extending a class

In extending a Thread class, methods and variables are inherited from the parent Thread class. In such a case, you are allowed to extend only a single class. To overcome such a situation, Java has provided you an interface to create threads. It is the most common way of creating threads.

Interfaces in java are very important and use to create template of a class. Interfaces are used for design purposes where a set of classes are implemented. The interface provides the design layout for a class and the class that implements an interface will provide all the functionality. All the classes that implement the interfaces must follow the same design rules.

Interfaces and classes contain only few differences. Classes implements methods and contain non constant variables where as interface contains only abstract methods or final static variables. Also an interface cannot implement a method. When a class implements an interface, it should provide functionality of all the methods and also it can implement more than one interface. An instance of an interface cannot be created with the new operator like an instance of a class.

Create Thread – Implement Runnable Interface

By implementing a Runnable interface, your class can be executed as a thread. Following are three basic steps to make a class a thread by implementing Runnable interface:

  1. First of all, it is required to provide functionality of run() method by the class that implements Runnable interface. The run() method is the entry point for the thread. All the code or business logic of the thread should be in this method. It is the start point and can call other classes or methods from this run() method. Following is the syntax of this method.
  2. A Thread object is created using following constructor.

    The obj is an instance of a class which has implemented Runnable interface and name is the name given to new thread.
  3. After creating a thread, it is started by calling the start() method. This method call actually calls the run() method of the class that has implemented Runnable interface. Following is start() method syntax.

Create Thread – Extend Thread Class

The other way to create a thread is to extend the Thread class. Following steps explains the process of creating a thread by extending the Thread class.

  1. The run() method is overridden by the class that extends the Thread class. It is the entry point for the thread and it contains all the business logic or call to other methods. Following is the run() method syntax.
  2. After creating the Thread object, the start() method is called to start the thread that eventually call the run() method. Following is the start() method syntax.

Thread Methods

Following table list all the important thread methods.

Method Description
 public void start() This method is responsible for starting a thread in a new path and invokes the run() method.
 public void run() This method is called when a thread is started using start() method. It is implemented by the class and logic is given in it.
 public final void setName(String name) This method sets the name of the newly created thread. This name is returned when getName() method is called n the thread.
 public final void setPriority(int priority) This method sets the priority of the thread. The values of the priority are between 1 and 10.
 public final void setDaemon(boolean on) This method is used to make a thread as daemon thread. A true value serves the purpose.
 public final void join(long millisec) This method cause to invoke a method on another thread to wait till its execution or of the time given in milliseconds.
 public void interrupt() This method is used to interrupt the thread. So that is continue its execution if it was blocked.
 public final boolean isAlive() This method return true if thread is still alive.

 


 

Leave a Reply

avatar
300
  Subscribe  
Notify of