📜  如何在Java中使用多线程查找质数和回文数?

📅  最后修改于: 2022-05-13 01:54:25.290000             🧑  作者: Mango

如何在Java中使用多线程查找质数和回文数?

Java中的多线程是同时执行两个或多个线程以最大限度地利用 CPU 的过程。多线程应用程序执行两个或多个并发运行的线程。因此,它在Java中也称为并发。每个线程彼此并行运行。多个线程不分配单独的内存区域,因此它们节省了内存。

问题陈述:编写一个双线程程序,其中一个线程查找所有质数(0 到 100),另一个线程查找所有回文数(10 到 1000)。以顺序方式调度这些线程以获得结果。现在将它们重新调度为并行线程。

解决方案:

同步块的工作原理:

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