我需要执行ping Web服务来检查我是否已连接到端点,并且Web服务服务器一切正常。
这有点愚蠢,但我必须为此打电话给网络服务。问题是,当我调用stub.ping(request)和我没有连接时,它会继续尝试执行此代码一分钟……然后返回false。
如果无法ping通,是否可以在1秒后使此超时?
public boolean ping() {
try {
PingServiceStub stub = new PingServiceStub(soapGWEndpoint);
ReqPing request = new ReqPing();
UserInfo userInfo = new UserInfo();
userInfo.setName(soapGWUser);
userInfo.setPassword(soapGWPassword);
ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.setConfigurationName(soapGWAppName);
stub.ping(request);
return true;
} catch (RemoteException | PingFault e) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我不知道为什么,但是我用来计算PI的单线程代码比多线程要快得多。我正在使用5亿点和用于多线程16核。如果使用单CPU很好并且使用多线程,则所有16个核都是100%,但是速度较慢...
有任何线索吗?
单
public static double monteCarloMethodSequencialMethod(long points) {
long inCircle = 0;
for (long i = 0; i < points; i++) {
double x = Math.random();
double y = Math.random();
if(x * x + y * y <= 1) inCircle++;
}
return 4.0 * inCircle / points;
}
Run Code Online (Sandbox Code Playgroud)
蒙特卡洛序列估计的PI值:3.141562496。在13432.927304 ms中执行。
多线程
public double calculatePI() throws InterruptedException, ExecutionException {
double sum = 0;
List<Future<Double>> tasks = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(nProcessors);
for (long i = 0; i < points; …Run Code Online (Sandbox Code Playgroud) 我知道我可以在String上添加左零,但是Long呢?
我需要输入零,直到Long的大小为10位。例如,如果是8位数字(12345678),则应添加2个左零(0012345678)
我想在getValue()方法中添加它。
public Long getValue() {
// Should always be 10 digits, If it's 8, add zeros
return value;
}
Run Code Online (Sandbox Code Playgroud)
我正在用弹簧。问题是数据库切掉了左零。也许有注释以避免多余的代码?
java ×3