【发布时间】:2015-06-09 13:38:54
【问题描述】:
我尝试使用多个线程,遗憾的是没有运气:
public synchronized boolean pingServer(final String ip, final short port) {
final boolean[] returnbol = new boolean[1];
Thread tt = new Thread(new Runnable() {
@Override
public void run() {
try {
Socket s = new Socket(ip, port);
s.close();
returnbol[0] = true;
} catch (IOException e) {
returnbol[0] = false;
}
}
});
tt.start();
try {
tt.join();
} catch (InterruptedException e) {
tt.stop();
}
tt.stop();
return returnbol[0];
}
由于某种原因,主线程仍然冻结。
是否有“无延迟”的方式来 ping 服务器?
【问题讨论】:
-
你认为
tt.join()是做什么的? -
您正在让当前线程等待
tt完成执行。 -
@Reimeus 我被用户误导了 tt.join();等任务完成。
-
如果你只是要等待它完成,为什么要启动一个线程来做某事?与其等待,不如自己动手。线程给你带来了什么?
标签: java multithreading sockets