I need to have a thread which checks for network connection availability on a JAVA desktop app. I got a thread like this
class DataSyncThread extends Thread {
DataSyncThread() {
}
public void run() {
while(true){
try{
System.out.println("Checking for network");
InetAddress addr = InetAddress.getByName(host);
if(addr.isReachable(MIN_PRIORITY)){
syncData();
}
this.sleep(1000000);
}catch(Exception e){}
}
}
}
Now when I call this in the constructer the app never loads. when I look into the console (I trigger the jar to load from it) the thread work, it prints “Checking for network” in the console.
help appreciated
My guess is that you’re doing something like:
That will run the
run()method synchronously. You should be callingstart()to create a separate thread of execution:I would also recommend implementing
Runnableinstead of extendingThread– or quite possibly using aTimerinstead, given that you want periodic execution. I hope your real code has logging in your catch block, too…