How to Find Prime and Palindrome Numbers using Multi-Threading in Java?


Multithreading is a feature of the Java programming language that allows us to perform multiple operations simultaneously. In it, the operation gets divided into multiple smaller parts called a thread. Each thread performs one independent task without affecting the other thread's performance. The main benefit of multithreading is the optimal use of resources like CPU and it boosts the execution time of allocated operations.

Finding prime and palindrome numbers are two of the basic programming task that every beginner programmer perfroms. However, in this article, we are going to do the same task in an exciting way. We will discuss a Java program to find prime and palindrome numbers in a multithreaded environment means by using threads. Here, threads mean small sub processes of a big operation.

Program to find Prime and Palindrome numbers using multithreading

We will follow the below approach to find prime and palindrome numbers using multithreading in Java.

Approach

  • Create a class named 'Thrd' and inside it define two static methods named 'operation1()' and 'operation2()' along with corresponding parameters.

  • Define the logic of palindrome number inside 'operation1()' and the logic of prime number inside 'operation2()'. Prime number is a whole number with only two factors 1 and the number itself, while a palindrome number reads the same backward as forwards.

  • Moving further, create two Thread classes. Inside the first thread class call the 'operation1()' method by passing an argument. Similarly, call the 'operation2()' method inside the second thread class.

  • At last, in the main method create two objects for the thread class and execute them using the inbuilt method 'start()'.

Example

class Thrd {   
   // method to find palindrome number
   public static void operation1(int num) {    
      int num1 = num;
      int rev = 0;
      while(num1 != 0) {
         int rem = num1 % 10;
         num1 /= 10;
         rev = rev * 10 + rem;
      }
      if(num == rev) {
         System.out.println(num + " is a Palindrome number");
      } else {
         System.out.println(num + " is Not a Palindrome number");
      }  
   }
   // method to find prime number
   public static void operation2(int nums) {
      int countr = 0;
      if(nums == 2) {
         System.out.println(nums + " is a prime number");
      } else {
         for(int i = 1; i <= nums; i++) {
            if(nums % i == 0) {
               countr++;
            }
         }
         if(countr == 2) {
            System.out.println(nums + " is a prime number");
         } else {
            System.out.println(nums + " is not a prime number");
         }
      }
   }
}    
class Thrd1 extends Thread {   // thread number 1 
   public void run() {    
      Thrd.operation1(212); // calling method to check palindrome number   
   }    
}    
class Thrd2 extends Thread { // thread number 2    
   public void run() {    
      Thrd.operation2(23); // calling the method to check prime number   
   }    
} 
public class ThrdExecution {    
   public static void main(String args[]) {    
      // creating object for thread class
      Thrd1 oprt1 = new Thrd1();    
      Thrd2 oprt2 = new Thrd2();  
      // Starting the thread operation
      oprt1.start();    
      oprt2.start();  
   }    
}

Output

23 is a prime number
212 is a Palindrome number

Conclusion

We started this article by introducing multithreading and threads. Then, we defined the problem statement and our goal, which is to find prime and palindrome numbers using multithreading. In the next section, we discussed the solution for the given problem with the help of an example program. In our solution, we created two separate threads to handle the operations of finding prime and palindrome numbers independently.

Updated on: 20-Jul-2023

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements