我一直在使用GCP计算引擎来运行置换计算器,该引擎被设计为可同时运行,因此应该在16核高CPU实例上产生1500%cpu的使用率;但是实际上是90%。没有同步,每个线程彼此完全独立地运行(它们甚至可以在不同的进程上运行,而不会影响最终结果)。
App.java:
package com.syd;
public class App {
public static void main(String[] args) {
int n;
int threads;
try{
n = Integer.parseInt(args[0]);
threads = Integer.parseInt(args[1]);
}catch(Exception e){
System.out.println("Default App Values!");
n=5;
threads =4;
}
System.out.printf("====%n %d CARDS%n====%n %d THREADS%n====%n", n, threads);
Chain[] chain = new Chain[threads];
for(int i=0;i<threads;i++){
chain[i] = new Chain(n,i,threads);
chain[i].start();
}
for(int i=0;i<threads;i++){
boolean complete = false;
while(!complete){
try{
chain[i].getThread().join();
complete = true;
}catch(Exception e){
e.printStackTrace();
}
}
}
new Add(n,threads).run();
}
}
Chain.java
package com.syd;
import java.io.*;
import java.io.File;
import java.math.BigInteger;
public class Chain implements Runnable{
private int n;
private int thread;
private int threads;
private Thread t;
public static void main(String[] args) {
try{
new Chain(Integer.parseInt(args[0]), Integer.parseInt(args[1]),Integer.parseInt(args[2])).run();
}catch(Exception e){
System.out.println("Default Chain Values!");
new Chain(5,0,4).run();
}
}
public Chain(int n, int thread, int threads){
this.n=n;
this.thread=thread;
this.threads=threads;
System.out.printf("Chain(%d,%d,%d)%n",n, thread, threads);
}
public void start(){
if(t==null){
t = new Thread(this);
t.start();
}
}
public Thread getThread(){
return t;
}
public void run(){
new File("./res/").mkdir();
String path = String.format("./res/%dn%dt%d.txt",n,thread,threads);
try{
PrintWriter writer = new PrintWriter(path, "UTF-8");
for(int i=0;i<10;i++){
writer.println("0");
}
writer.flush();
writer.close();
}catch(Exception e){
e.printStackTrace();
}
BigInteger maxPerms = new BigInteger("1");
for(int i=52;i>52-n;i--){
maxPerms = maxPerms.multiply(new BigInteger(String.format("%d",i)));
}
//System.out.printf("maxPerms:%s%n",maxPerms);
BigInteger max = (maxPerms.multiply(new BigInteger(String.format("%d", thread+1)))).divide(new BigInteger(new Integer(threads).toString()));
BigInteger current = (maxPerms.multiply(new BigInteger(String.format("%d", thread)))).divide(new BigInteger(new Integer(threads).toString()));
do{
BigInteger from = current;
current = current.add(new BigInteger("5000000"));
if (current.subtract(max).doubleValue()>0){
current = max;
}
System.out.print(".");
new Worker(n, from, current, path).run();
}while(current.subtract(max).doubleValue()<0);
}
}
运行
sudo nohup java -cp . com.syd.App 5 4
产生90%cpu
运行
sudo nohup java -cp . com.syd.Chain 5 0 4
(目前,该进程的cpu为90%),然后是
sudo nohup java -cp . com.syd.Chain 5 1 4
(在另一个终端窗口中)每个进程产生45%cpu
我认为它可能是针对谷歌云平台的一些配置,以启用其他内核,因为命令顶部仅显示一个cpu。
感谢您的帮助, 谢谢