0% found this document useful (0 votes)
8 views3 pages

Practical - 1 (Thread Synchronization) Using JAVA

Uploaded by

mridulddubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Practical - 1 (Thread Synchronization) Using JAVA

Uploaded by

mridulddubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical -1 (Thread Synchronization) Using JAVA

CODE

// 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
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();
}
}
}
OUTPUT:

1 22 33 44 55 66 77 88 99 101 111 121


131
141 151 161 171 181 191
202
92 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464
474
484
55 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727
737 747 757
18 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979
989
999
Prime numbers from 0 to 100 :
35
7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

You might also like