How to Find Prime and Palindrome Numbers using Multi-Threading in Java?
Last Updated :
03 Aug, 2021
Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other. Multiple threads don't allocate separate memory areas, hence they save memory.
Problem Statement: Write a two-threaded program, where one thread finds all prime numbers (in 0 to 100) and another thread finds all palindrome numbers (in 10 to 1000). Schedule these threads in a sequential manner to get the results. Now reschedule them as parallel threads.
Solution:
1. Two threads initiated, one thread to print prime numbers and another to print palindrome numbers
2. Algorithm to print prime numbers :
START
Step 1 → Take integer variable A
Step 2 → Divide the variable A with (A-1 to 2)
Step 3 → If A is divisible by any value (A-1 to 2) it is not prime
Step 4 → Else it is prime
STOP
3. Algorithm to print palindrome number
START
Step 1 → Get the number from user
Step 2 → Hold the number in temporary variable
Step 3 → Reverse the number
Step 4 → Compare the temporary number with reversed number
Step 5 → If both numbers are same, print.
Stop
How synchronization block works :
thread 1, thread 2, thread 3 ---> synchronization ---> thread 1
thread 2,thread 3 ---> synchronization ---> thread 2
Java
import java.util.Scanner;
// thread to print prime numbers
class part1 extends Thread {
public synchronized void run()
{
int i = 0;
int num = 0;
String primeNumbers = "";
for (i = 1; i <= 100; i++) {
int counter = 0;
for (num = i; num >= 1; num--) {
// condition to check if the number is prime
if (i % num == 0) {
// increment counter
counter = counter + 1;
}
}
if (counter == 2) {
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("\nPrime numbers from 0 to 100 : \n"
+ primeNumbers);
System.out.println();
}
}
// thread to print palindrome numbers
class part2 extends Thread {
public synchronized void run()
{
int n, b, rev = 0;
int N = 1000;
System.out.println("Palindrome numbers from 10 to 1000 : ");
for (int i = 10; i <= N; i++) {
n = i;
while (n > 0) {
// Find reverse of n
b = n % 10;
rev = rev * 10 + b;
n = n / 10;
}
// If n and rev are same, n is palindrome number
if (rev == i) {
System.out.print(i + " ");
}
rev = 0;
}
}
}
public class Main {
public static void main(String args[])
{
part1 t1 = new part1();
part2 t2 = new part2();
Thread m1 = new Thread(t1);
Thread m3 = new Thread(t2);
Scanner sc = new Scanner(System.in);
// start() method starts the execution of thread.
m1.start();
m3.start();
try {
// join() method waits for the thread to die
m1.join();
m3.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
OutputPalindrome numbers from 10 to 1000 :
11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999
Prime numbers from 0 to 100 :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Similar Reads
Print even and odd numbers in increasing order using two threads in Java Given an integer N, the task is to write Java Program to print the first N natural numbers in increasing order using two threads.Prerequisite: MultithreadingExamples:Input: N = 10Output: 1 2 3 4 5 6 7 8 9 10Input: N = 18Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18Approach: The idea is to cre
3 min read
Java Program to Create a Matrix and Fill it with Prime Numbers Prime Numbers are the number which can be divisible by only 1 and itself, it cannot be divisible by any other number. The task here is to find the prime numbers and store them in the form of a matrix. Example: Prime numbers are: 2, 3, 5, 7 Output: 2 3 5 7 Prime numbers : 2, 3, 5, 7, 9, 11, 13, 17, 1
3 min read
How to Perform Parallel Processing on Arrays in Java Using Streams? In Java, parallel processing helps to increase overall performance. Arrays may be processed in parallel in Java by using Streams API. When working with big datasets or computationally demanding tasks, this is very helpful. In this article, we will learn how to perform parallel processing on arrays i
2 min read
Palindrome Number Program in Java A given number can be said to be palindromic if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.Example of Palindrome Number:Input : n = 121Output: Reverse of n = 121Palindrome : YesIn
7 min read
Java Program to Display All Prime Numbers from 1 to N For a given number N, the purpose is to find all the prime numbers from 1 to N.Examples: Input: N = 11Output: 2, 3, 5, 7, 11Input: N = 7Output: 2, 3, 5, 7 Approach 1:Firstly, consider the given number N as input.Then apply a for loop in order to iterate the numbers from 1 to N.At last, check if each
4 min read