java代码运行后没结果??
第一个类publicclassPrimeFinderimplementsRunnable{publiclongtarget;publiclongprime;publicb...
第一个类
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();
}
}
@Override
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;
}
}
第二个类,主类
public class PrimeThreads {
public static void main(String[] args){
PrimeThreads pt = new PrimeThreads(args);
}
public PrimeThreads(String[] args){
PrimeFinder[] finder = new PrimeFinder[args.length];
for(int i = 0; i < args.length; i++){
try{
long count = Long.parseLong(args[i]);
finder[i] = new PrimeFinder(count);
System.out.println("Looking for prime " + count);
}catch(NumberFormatException nfe){
System.out.println("Error: " + nfe.getMessage());
}
}
boolean complete = false;
while(!complete){
complete = true;
for(int j = 0; j < finder.length; j++){
if(finder[j] == null) continue;
if(!finder[j].finished){
complete = false;
}else{
displayResult(finder[j]);
finder[j] = null;
}
}
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
// do nothing
}
}
}
private void displayResult(PrimeFinder finder){
System.out.println("Prime " + finder.target + " is " + finder.prime);
}
}
运行之后没输出如何结果
run:
成功构建 (总时间: 1 秒)
这是为什么?? 展开
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();
}
}
@Override
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;
}
}
第二个类,主类
public class PrimeThreads {
public static void main(String[] args){
PrimeThreads pt = new PrimeThreads(args);
}
public PrimeThreads(String[] args){
PrimeFinder[] finder = new PrimeFinder[args.length];
for(int i = 0; i < args.length; i++){
try{
long count = Long.parseLong(args[i]);
finder[i] = new PrimeFinder(count);
System.out.println("Looking for prime " + count);
}catch(NumberFormatException nfe){
System.out.println("Error: " + nfe.getMessage());
}
}
boolean complete = false;
while(!complete){
complete = true;
for(int j = 0; j < finder.length; j++){
if(finder[j] == null) continue;
if(!finder[j].finished){
complete = false;
}else{
displayResult(finder[j]);
finder[j] = null;
}
}
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
// do nothing
}
}
}
private void displayResult(PrimeFinder finder){
System.out.println("Prime " + finder.target + " is " + finder.prime);
}
}
运行之后没输出如何结果
run:
成功构建 (总时间: 1 秒)
这是为什么?? 展开
2个回答
展开全部
你的main方法里只有一行代码,new 了一个PrimeThreads,参数是字符串数组 args
PrimeThreads pt = new PrimeThreads(args);
调用PrimeThreads的构造方法
public PrimeThreads(String[] args) {
PrimeFinder[] finder = new PrimeFinder[args.length];
for (int i = 0; i < args.length; i++) {
//……
注意这里有个字符串数组参数:args
PrimeThreads的构造方法内部的所有实现都是围绕着args的,如果args没有元素也就是args.length() == 0 ,那么第一个for循环直接跳过,同时finder也没有元素,第二个for循环也直接跳过,然后在sleep那里睡眠1秒,while循环也退出,程序结束。于是出现了你说的总时间1秒。
在来说说args,这个参数最早是在main方法中传进来的
public static void main(String[] args){
其实这个参数默认情况是没元素的是空的
试试给PrimeFinder构造方法传值看看效果
可以看出来你描述的运行之后没输出的原因即是args为空
关于如何给args赋值你可以自行百度,这里贴一个刚刚百度到的链接
http://jingyan.baidu.com/article/86f4a73e63862a37d6526909.html
2015-09-10
展开全部
那到底这段代码的需求是什么样的呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询