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(); } } } |
Palindrome 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