I am calling a thread inside while loop to sleep for 1 sec. While the flag is true, loop will be running (flag is true for infinite time). Inside the loop thread should sleep for 1 second, wakes up and increase the counter, checks the IF condition and on FALSE condition it should sleep again for 1 sec and the process continues 29 times. On the 30th iteration IF condition will be true and the method called inside IF statement will collect and store a data. Finally on the 32nd iteration method called inside second IF statement will send the stored data to the server and sets the count = 0. The problem is, sometimes sleep thread is sleeping for more than 1 min or sleeps for indefinite time period. Find here my enclosed piece of code.
public class NetworkThread implements Runnable {
private boolean flag;
public NetworkThread(boolean bool) {
flag = bool;
isrunning();
}
private boolean isrunning() {
return flag;
}
int counter = 0;
@Override
public void run() {
sendStartPacket();
while (flag) {
try {
Thread.sleep(1000);
counter++;
if (counter % 30 == 0) {
// TODO Auto-generated method store an information
}
if (counter % 32 == 0) {
// TODO Auto-generated method send the information to server
}
} catch (Exception e) {
e.toString();
}
}
}
private void sendStartPacket() {
// TODO Auto-generated method stub
}