PrimerFinder.java:
package okhttp3.benchmarks;
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 static void main(String[] args) {
System.out.println(new PrimeFinder(0).isPrime(97));
}
}
PrimerThreads.java:
package okhttp3.benchmarks;
public class PrimeThreads {
public static void main(String[] args) {
PrimeThreads pt = new PrimeThreads(args);
//第两百五十万个素数41161739
//System.out.println(new PrimeFinder(0).isPrime(41161739));
}
public PrimeThreads(String[] arguments){
PrimeFinder[] finder = new PrimeFinder[arguments.length];
for(int i = 0;i <arguments.length;i++){
try {
long count = Long.parseLong(arguments[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);
}
}
运行结果:
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
.url("https://github.com/hongyangAndroid")
.build();
//new call
Call call = mOkHttpClient.newCall(request);
//请求加入调度
call.enqueue(new Callback()
{
@Override
public void onFailure(Request request, IOException e)
{
}
@Override
public void onResponse(final Response response) throws IOException
{
//String htmlStr = response.body().string();
}
});
网友评论