package com.java21days; public class PrimeFinder implements Runnable { public long target; public long prime; public boolean finished = false; private Thread runner; PrimeFinder(long inTarget) { target = inTarget; if (runner == null) { runner = new Thread(this); runner.start(); } } public void run() { long numPrimes = 0; long candidate = 2; while (numPrimes < target) { if (isPrime(candidate)) { numPrimes++; prime = candidate; } candidate++; } finished = true; } boolean isPrime(long checkNumber) { double root = Math.sqrt(checkNumber); for (int i = 2; i <= root; i++) { if (checkNumber % i == 0) return false; } return true; } }
时间: 2024-02-14 18:35:53 浏览: 23
这段代码是一个实现了 Runnable 接口的类 PrimeFinder,它用于查找一个指定数量的质数。在构造方法中,传入一个 long 类型的参数用于指定要查找的质数的数量。然后在构造方法中创建一个新的线程并启动它,在该线程的 run() 方法中查找质数。isPrime() 方法用于判断一个数是否为质数,如果是,则将其存储到 prime 变量中,并将其数量 numPrimes 加 1。该类还有一个 boolean 类型的 finished 变量,用于表示线程是否完成了任务。
相关问题
public class studentPojo extends BasePojo implements Serializable { private static final long serialVersionUID = 1L;
这是一个 Java 类的定义,它继承了一个名为 BasePojo 的基类,并实现了 Serializable 接口。其中,serialVersionUID 是一个序列化版本号,用于在反序列化时检查版本的一致性。
package com.moreTest.test04; import java.util.Random; public class MyRun04 implements Runnable { public static String[] arr = new String[3]; public MyRun04() { Random r = new Random(); /*arr[0] = String.format("%.2f", r.nextDou
我注意到你的问题中缺少了一个问题,但是我还是能够回答你提出的问题。根据你所给的代码,MyRun04 是一个实现了 Runnable 接口的类,其中有一个静态的 String 数组 arr。在该类的构造函数中,使用 Random 类生成一个随机数,并将该随机数格式化为一个两位小数的字符串,并将其赋值给 arr 数组的第一个元素。但是,由于你没有提供完整的代码,我无法确定该类的作用和运行方式。如果你有任何其他的问题,请继续提出。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)