import java.math.*;
import java.util.*;
class multi_track implements Runnable
{
static int NUM=0;
static int temp=2;
static int N=100;
public boolean judge_prime(int N)
{
if(N==2)return true;
if(N%2==0)
return false;
for(int i=3;i<=Math.sqrt(N);i+=2)
{
if(N%i==0)
return false;
}
return true;
}
public void run()
{
multi_track sin=new multi_track();
for(;temp<N;temp++)
if(sin.judge_prime(temp)==true)
{
System.out.println(temp);
NUM++;
System.out.println(Thread.currentThread());
}
}
public static void main(String[] args)
{
long startTime=System.nanoTime();
Runnable Thread_1=new multi_track();
Runnable Thread_2=new multi_track();
Thread t1=new Thread(Thread_1);
Thread t2=new Thread(Thread_2);
t1.start();
t2.start();
while(t1.isAlive()||t2.isAlive());
System.out.println(NUM);
System.out.println(temp);
long endTime=System.nanoTime();
System.out.println(endTime-startTime);
}
}
import java.util.*;
class multi_track implements Runnable
{
static int NUM=0;
static int temp=2;
static int N=100;
public boolean judge_prime(int N)
{
if(N==2)return true;
if(N%2==0)
return false;
for(int i=3;i<=Math.sqrt(N);i+=2)
{
if(N%i==0)
return false;
}
return true;
}
public void run()
{
multi_track sin=new multi_track();
for(;temp<N;temp++)
if(sin.judge_prime(temp)==true)
{
System.out.println(temp);
NUM++;
System.out.println(Thread.currentThread());
}
}
public static void main(String[] args)
{
long startTime=System.nanoTime();
Runnable Thread_1=new multi_track();
Runnable Thread_2=new multi_track();
Thread t1=new Thread(Thread_1);
Thread t2=new Thread(Thread_2);
t1.start();
t2.start();
while(t1.isAlive()||t2.isAlive());
System.out.println(NUM);
System.out.println(temp);
long endTime=System.nanoTime();
System.out.println(endTime-startTime);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货