zoukankan      html  css  js  c++  java
  • java多线程找素数实例

     1 package ltb20180106;
     2 
     3 public class FindPrime implements Runnable{
     4 
     5     private int prime;
     6     private int q;
     7     private int w;
     8     
     9     public FindPrime(int nn,int mm) {
    10         
    11          q=nn;
    12          w=mm;
    13     }
    14 
    15     public void run() {
    16         
    17          getFindPrime(q,w);
    18          
    19     }
    20     
    21     public synchronized void getFindPrime(int min,int max) {
    22         
    23            int  k=max;
    24            int  j=min;
    25              
    26             if(max<min) {
    27                 
    28                 System.out.println("最大值不能小于最小值,请重新输入!");
    29                 
    30             }else if(max<=0||min<0) {
    31                 
    32                 System.out.println("输入值不能是负数,或者最大值不能是零.");
    33             }
    34         
    35         for(int i=k;i>j;i--) {
    36             
    37             if(i==1) { //1不是素数
    38                 
    39                 continue;
    40             }
    41             
    42             prime=i%2;//去掉偶数
    43             
    44             if(prime==0&&i>3) {
    45                 
    46                 continue;
    47             }
    48             
    49             prime=i%3;//去掉整除3的数,但是要大于7
    50             
    51             if(prime==0&&i>7) {
    52                 
    53                 continue;
    54                 
    55             }
    56             
    57             prime=i%5;//去掉整除5的数,但是要大于7
    58             
    59             
    60              if(prime==0&&i>7) {
    61                 
    62                 continue;
    63                 
    64             }
    65             
    66             prime=i%7;//去掉整除7的数,,但是要大于7
    67             
    68 
    69             if(prime==0&&i>7) {
    70                 
    71                 continue;
    72                 
    73             }
    74             
    75              
    76             System.out.println("["+Thread.currentThread().getName()+"]"+"素数:"+i+"["+prime+"]");
    77             
    78             
    79         }
    80         
    81     }
    82     
    83     public static void main(String[] args) {
    84         
    85         Thread t1=new Thread(new FindPrime(0,1000)); 
    86        t1.start();
    87        Thread t2=new Thread(new FindPrime(1001,2000));
    88        t2.start();
    89         Thread t3=new Thread(new FindPrime(2001,3000));
    90        t3.start();
    91         
    92     }
    93 
    94 } 
  • 相关阅读:
    浏览器(WebRTC) 如何获取音视频流
    在linux shell中,用$#表示传递进来的参数个数
    Vi很省力
    Factorial! You Must be Kidding!!!(ICPC冬令营集训题目)
    Pig-Latin(ICPC冬令营集训题目)
    C# .net
    编辑器
    VC 和 VS 区别
    Visual Studio和Visual Studio Code差异篇
    IDE 与 编辑器的对比
  • 原文地址:https://www.cnblogs.com/ltb6w/p/8458576.html
Copyright © 2011-2022 走看看