Understanding Multithreading in Java : Actual Use

Understanding Multithreading in Java : Actual Use

Introduction

·

2 min read

Multithreading is a powerful concept in Java that allows multiple threads to run concurrently, improving performance and efficiency.

In this blog, we will explore multithreading with a real-world example: checking if a string is a palindrome using multiple threads. This will help you understand how to execute tasks in parallel and optimize performance.

What is Multithreading?

Multithreading is the ability of a CPU to execute multiple threads simultaneously. It helps in:

  • Enhancing application performance.

  • Efficiently utilizing system resources.

  • Performing multiple tasks in parallel.

A thread is a lightweight sub-process. In Java, threads can be created in two ways:

  1. Extending the Thread class.

  2. Implementing the Runnable interface.

Real-World Example: Checking for a Palindrome

A palindrome is a string that reads the same forward and backward, such as madam or racecar. We will use two threads to:

  1. Reverse the string.

  2. Check if the reversed string matches the original string.

    Approach:

    • Use two threads:

      1. One thread (ReverseThread) reverses the string.

      2. Another thread (CheckPalindromeThread) checks if the original and reversed strings are the same.

    class ReverseThread extends Thread {
        private String str;
        private StringBuilder reversedStr;

        public ReverseThread(String str) {
            this.str = str;
            this.reversedStr = new StringBuilder();
        }

        @Override
        public void run() {
            reversedStr.append(new StringBuilder(str).reverse());
        }

        public String getReversedStr() {
            return reversedStr.toString();
        }
    }

    class CheckPalindromeThread extends Thread {
        private String original;
        private String reversed;

        public CheckPalindromeThread(String original, String reversed) {
            this.original = original;
            this.reversed = reversed;
        }

        @Override
        public void run() {
            if (original.equals(reversed)) {
                System.out.println(original + " is a palindrome.");
            } else {
                System.out.println(original + " is NOT a palindrome.");
            }
        }
    }

    public class PalindromeMultiThread {
        public static void main(String[] args) throws InterruptedException {
            String str = "madam"; // Change this to test different strings

            // First thread to reverse the string
            ReverseThread reverseThread = new ReverseThread(str);
            reverseThread.start();
            reverseThread.join(); // Ensure it completes before moving forward

            // Second thread to check palindrome
            CheckPalindromeThread checkThread = new CheckPalindromeThread(str, reverseThread.getReversedStr());
            checkThread.start();
            checkThread.join(); // Ensure it completes before exiting
        }
    }

Explanation:

  1. ReverseThread reverses the input string.

  2. CheckPalindromeThread compares the original and reversed strings.

  3. Main Method:

    • Starts ReverseThread and waits for it to finish (join()).

    • Starts CheckPalindromeThread to check if the string is a palindrome.