Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

Simplified Learning Blog

Learning made easy

  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • Spring Boot Tutorial
      • Spring Boot Rest API Example complete guide
    • Top 50 Java Interview Questions
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
    • Java Thread Tutorials
      • How to create thread in Java
      • Multithreading in java
      • Daemon Thread in Java | How to create daemon thread in java
      • Top 40+ Multithreading interview questions
  • AWS
    • What is AWS (Amazon Web Services)
    • AWS IAM (Identity and Access Management)
    • AWS SNS | What is SNS
    • What is SQS | AWS SQS (Simple Queue Service)
    • What is AWS Lambda
    • Top 10 AWS Lambda interview questions
  • Java Codes
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • System Performance Objective
  • Spring Boot Tutorial
  • Tools
    • JSON Formatter and Validator
  • Tech Blogs
    • Java 21 New Features
    • Is Java Dead? Is java dead, 2023 ?
    • New Features in Java 17
  • Toggle search form

Multithreading in java

Posted on February 13, 2023February 27, 2024 By Admin No Comments on Multithreading in java

What is multithreading in java

Introduction: What is Multithreading

Multithreading in java refers to processing or executing multiple threads simultaneously for maximum utilization of CPU. Multithreading applications runs two or more processes concurrently.

Table of Contents

Toggle
  • What is multithreading in java
  • Introduction: What is Multithreading
  • Concurrency
  • What is Concurrency?
    • Multiprocessing
    • Multitasking
    • Multithreading
  • Multithreading in Java Example:
  • Multithreading in java real time example

Java Thread class allows us to create one or more lightweight threads and java executes them in parallel. The CPU of our system has access to both the memory and hardware, so it operates on both to process the tasks. The CPU core interprets the instruction and executes them and if we have more cores then more processing is happening behind the scene. If we have only one CPU in that case, then your computer still works by doing tasks switching. In tasks switching, CPU switches between tasks and performs the multiprocessing.

To know more/basic of threading, please read this tutorial first.

Concurrency

What is multithreading in java
Concurrency VS Parallelism

What is Concurrency?

Concurrency is the ability to do more than one thing at the same time. Running multiple parts of a single application simultaneously is also termed as concurrency.

Example:

MS Word processor formats the text and responding to keyboard and mouse events.

Concurrency doesn’t mean parallel execution. Multiple tasks executing at the same time means these multiple tasks are making progress during the same period of time. The tasks are executed in an interleaved manner. The OS switches between the tasks so frequently that it appears that they are being executed at the same time. Therefore, we can not say concurrency means parallelism.

Multiprocessing

Multiple processors/CPUs executing concurrently. Processor i.e. CPU is concurrent here.

Multitasking

Multiple tasks/process running concurrently on single CPU. The OS executes these tasks by switching in between them very frequently. Process is concurrent here.

Multithreading

Multiple parts of the same program running concurrently. In this case, we divide the same program into multiple parts/threads and going to run these threads concurrently.

Multithreading in Java Example:

package com.slb.thread;

class MultiThreadingDemo extends Thread{

    @Override
    public void run() {
        System.out.println("Thread is running.." + Thread.currentThread().getName());
    }
}

public class MultiThreadMain{

    public static void main(String[] args) {
        int numberOfThread = 10;

        for (int i = 0; i < numberOfThread; i++){
            MultiThreadingDemo multiThreadingDemo = new MultiThreadingDemo();
            multiThreadingDemo.setName("Thread " + i);
            multiThreadingDemo.start();
        }
    }
}

Output:

Thread is running..Thread 1
Thread is running..Thread 3
Thread is running..Thread 7
Thread is running..Thread 5
Thread is running..Thread 4
Thread is running..Thread 2
Thread is running..Thread 8
Thread is running..Thread 9
Thread is running..Thread 6
Thread is running..Thread 0

Process finished with exit code 0

In the above code snippet, we are creating multiple threads (10 threads we are creating). In a loop we are creating an object of a thread naming it and calling the start method on it, however you might see different output when you run this program because it depends on the which thread gets the processor and at what time.

Multithreading in java real time example

Let’s take an example of multithreading in real time:

1. Computer games: Let’s take an example of a computer game where in a lot of threads are running with some of the tasks like playing music as per character’s movement, character movements, if he is driving a car in the game then process of running the car itself is taking care by one of the tread. Similarly, in case of shooting a gun while driving same car can be handled by another thread.

2. Web Browsers: When we browse the browser it handles multiple things at the same time as we open different tabs, then on some tabs we start downloading something, etc.

3. Text Editors: While typing on the text editors we might get suggestions on spelling mistakes that is also one of the daily used real time multithreading example.

4. IDE: We have been using IDE for day to day development activity in which we write the code and at the same time a lot of other threads are processing small activities in the background like issue checks, compilation checks, builds, etc.

Related

Java Threads

Post navigation

Previous Post: Is Java Dead? Is java dead, 2023 ?
Next Post: Top 40+ Multithreading interview questions

More Related Articles

Daemon Thread in Java | How to create daemon thread in java Java Threads
Java Thread Tutorials Java Threads
Spring Boot Rest API Example complete guide Java Threads
How to create thread in Java Java Threads
Top 40+ Multithreading interview questions Java Threads

Leave a Reply Cancel reply

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

Recent Posts

  • Top 50 Java Coding Interview Questions and Answers (2025 Updated)
  • Java Record Class Explained: Simple, Immutable Data Carriers
  • Java Convert int to String – 5+ Methods with Examples
  • String to Integer Conversion in Java | Java convert string to int
  • PDF to JSON Convertor

Recent Comments

No comments to show.

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook Green WordPress theme