(ブール型)を実装するクラスからブール値を割り当てるプログラムがあります。しかし、プログラムを実行すると、クラスからブール値を割り当てた後にスタックします。
これが私のコードです:
クラス:
public static class TCP_Ping implements Callable<Boolean> {
private final BufferedReader in;
private final PrintWriter out;
private final String ip;
private final int port;
private final int time;
public TCP_Ping(BufferedReader _in, PrintWriter _out, String _ip,
int _port, int _time) {
this.in = _in;
this.out = _out;
this.ip = _ip;
this.port = _port;
this.time = _time;
}
@Override
public Boolean call() {
boolean returnValue = false;
String address = ip + ":" + port;
long before = System.nanoTime();
out.println(new byte[64]);
System.out.println("(" + time + ") Sent 64 bytes of data to "
+ address + "...");
try {
if ((in.readLine()) != null) {
int size = in.readLine().toString().getBytes().length;
long after = System.nanoTime();
long s = ((after - before) / 1000000L) / 1000;
System.out.println("(" + time + ") Recieved reply from "
+ address + " (" + size + " bytes), time = " + s
+ " seconds...");
returnValue = true;
} else if ((in.readLine()) == null) {
long after = System.nanoTime();
long s = ((after - before) / 1000000L) / 1000;
System.out.println("(" + time
+ ") Failed to recieve reply from " + address
+ ", time = " + s + " seconds...");
returnValue = false;
}
} catch (IOException exc) {
long after = System.nanoTime();
long s = ((after - before) / 1000000L) / 1000;
System.err.println("(" + time
+ ") Failed to recieve reply from " + address
+ ", time = " + s + " seconds...");
returnValue = false;
}
return returnValue;
}
}
ブール値が割り当てられる場所:
System.out.println("Starting ping...");
System.out.println("Will ping " + address
+ " with 64 bytes of data...");
long start = System.currentTimeMillis();
final FutureTask<Boolean> ft1 = new FutureTask<Boolean>(
new TCP_Ping(in, out, ip, port, 1));
Thread t1 = new Thread(ft1);
t1.start();
try {
while (t1.isAlive()) {
t1.join(20000);
if (((System.currentTimeMillis() - start) >= 20000)
&& t1.isAlive()) {
t1.interrupt();
System.out
.println("(1) - Failed to receive reply from "
+ address
+ ", since the ping timed out, time = 20 seconds...");
result = false;
break;
}
break;
}
} catch (InterruptedException exc) {
System.err
.println("(1) - An interuption occurred during a ping to "
+ address + "...");
}
try {
result = ft1.get();
} catch (Exception exc) {
System.err.println("Uh, oh! An internal error occurred!");
}