Home / Posts / Multithreading In Java
Multithread life cycle
Multithread Life Cycle

Multithreading In Java

Hi techies, in this blog I am going to explain what is multithreading and what are its uses. Before going to discuss about multithreading first we need to know about what is thread.

Java is multithreaded programming language it is one of its feature.

A thread is a light weight sub process, a smallest unit of processing. A thread is the path followed when executing a program. All java programs have at least one thread that is main thread. This main thread was created by JVM at the time of program start, when the main() method is invoked with the main thread. Threads are accessed shared data.

Multithreading is a process of executing multiple threads simultaneously.  Java is a multithreaded programming language.

Concurrency and parallelism:  The processor can switch execution resources between threads in a multithreaded process on a single process this was resulting in concurrent execution.

In parallel execution, multithreaded process in a shared memory multiprocessor environment, each thread in the process can run on a separate processor at the same time.

If the processors number is high when compared to threads each thread runs on a different processor.

Multithreading Definition:

A thread is a sequence of control within a process. A single-threaded process follows a single sequence of control while executing. An MT process has several sequences of control, thus is capable of several independent actions at the same time. When multiple processors are available, those concurrent but independent actions can take place in parallel.

Structure of Multithreading:  Traditional UNIX operating system supports the concept of threads, each process has a process id and a single thread, so programming with multiple processes is programming with multiple threads.  Each process has an address space and process id. Creating a thread is much less expensive when compared to creating a new process, because the newly created thread uses the current process address space.  Time for Switching between one thread to another is less when compare to process switching.

Threads communication is simple because threads are shared everything i.e address space and memory. Data produced by once thread is immediately available by another thread or all other threads.

The interface to support multithreading by a subroutine library, Lipthread for POSIX threads and Libthread for Solaris threads.

Multithreading provides flexibility by decoupling kernel-level and user-level resources.

Benefits of Multithreading:

  • Multithreading allows both the full exploitation f parallel hardware and the effective use of multiple processor subsystems.
  • Multithreading provides performance benefits on uniprocessor systems by improving overlap of operations such as computation and I/O.
  • Improves throughput of the system. In a single process many I/O requests are handled.
  • Simultaneous and fully symmetric use of multiple processor for computation and I/O.
  • It improves the server request handling efficiency.
  • Complex /large requests don’t block other requests.
  • Increases the superior application responsiveness. If a request can be launched on its own thread, applications do not freeze or show the “hourglass”. An entire application will not block, or otherwise wait, pending the completion of another request.
  • Better communication.
  • Threads can be simplify the structure of the complex applications.
  • Multithreading minimizes the system resource usage.
  • Threads impose minimal impact on system resources. Threads require less overhead to create, maintain, and manage than a traditional process.

Types of Multithreading:

  1. One to One
  2. Many to One
  3. Many to Many

 

Life Cycle of Multithreading in Java:

When a thread created the thread can be in one of the five states.  SUN specifies a thread has 4 states in its life cycle those are New, Runnable, Not Runnable and Terminated. These are specified by the SUN.

But for our convenience we learnt these are five states  New, Runnable, Not Runnable, Running and Terminated.

Multithread life cycle
Multithread Life Cycle

Demo Program for Multi Thread – By Using Runnable Interface:

public class ThreadbyRunnableInterface implements Runnable {

Thread t;
static String str = “java”;

public ThreadbyRunnableInterface(String str) {
t = new Thread(this, str);
t.start();
}

public static void main(String[] args) {
ThreadbyRunnableInterface tr = new ThreadbyRunnableInterface(str);
System.out.println(“In main” + Thread.currentThread());
}

@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(“Thread A” + t.currentThread());
for (int i = 0; i <= 5; i++) {
System.out.println(i + “”);
}

}
}

Output of the above program is:

In mainThread[main,5,main] Thread AThread[java,5,main] 0
1
2
3
4
5

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

Google Keyword Planner

How to Use Google Key Word Planner

Google Keyword Planner is a free tool provided by Google Ads to help businesses and …