Saturday, July 23, 2011

checking long running task and timing out using java thread


public boolean checkInBackground() throws InterruptedException {
final StringBuffer st = new StringBuffer();
Thread thread = new Thread() {
public void run() {
try {
check();
st.append("done");
} catch (Exception e) {
e.printStackTrace();
}
}

public void check() {
//long running task
while(true) {}
};
};
thread.setDaemon(true);
thread.start();
thread.join(2 * 1000);
if (st.toString().equals("done")) {
return true;
}
return false;
}

No comments:

Post a Comment