Multithreading in Java

What is Multithreading?

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process. … Start () invokes the run () method on the Thread object.


What are the advantages of multithreading?

The primary function of multithreading is to simultaneously run or execute multiple tasks. These tasks are represented as threads in a Java program and have a separate execution path. Also, handling of multithreaded Java programs is easy because you can decide the sequence in which execution of Java threads take place

Why do we use threads?

Thread is a light weight process which helps in running the tasks in parallel. The threads work independently and provides the maximum utilization of the CPU, thus enhancing the CPU performance. Threads to make Java application faster by doing multiple things at same time.

How does multithreading works?

Java multithreading allows you to do multiple tasks at the same time. This is possible because modern day computers have multiple CPUs (CPUs are the brain of your computer, and it has a bunch!). One CPU can work on one Thread at a time (unless your CPUs have hyper-threading, in which case it can handle two at a time)

Advantages of multithreading:

  • Enhanced performance by decreased development time
  • Simplified and streamlined program coding
  • Improvised GUI responsiveness
  • Simultaneous and parallelized occurrence of tasks
  • Better use of cache storage by utilization of resources
  • Decreased cost of maintenance
  • Better use of CPU resource

Disadvantages of multithreading:

  • Complex debugging and testing processes
  • Overhead switching of context
  • Increased potential for deadlock occurrence
  • Increased difficulty level in writing a program
  • Unpredictable results

Creating a thread in Java

There are two ways to create a thread in Java:
1) By extending Thread class.
2) By implementing Runnable interface.

Before we begin with the programs (code) of creating threads, let’s have a look at these methods of Thread class. We have used few of these methods in the example below.

  • getName(): It is used for Obtaining a thread’s name
  • getPriority(): Obtain a thread’s priority
  • isAlive(): Determine if a thread is still running
  • join(): Wait for a thread to terminate
  • run(): Entry point for the thread
  • sleep(): suspend a thread for a period of time
  • start(): start a thread by calling its run() method

Method 1: Thread creation by extending Thread class4

Example 1:


Example 2:



Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

View all posts by

Leave a Reply

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