I want to wait for a specific time to execute a function but I get the
Error: Thread[2,tid=3699,WaitingInMainSignalCatcherLoop,Thread*=0xa9f2c500,peer=0x36c090a0,"Signal Catcher"]: reacting to signal 3
during the "sleepUntil" function:
try {
TimeInMillis = getTimeInMillis();
currentTimeMillis = System.currentTimeMillis();
sleepUntil(TimeInMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
Function to calculate time in milliseconds:
public long getTimeInMillis(){
long TimeInMillis = 0;
Date date = new Date();
currentDate = dateformatter.format(date);
updateTime = currentDate + " " + updateTime;
try {
parseTime = formatter.parse(updateTime);
TimeInMillis = parseTime.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(TimeInMillis);
System.out.println(System.currentTimeMillis());
return TimeInMillis;
}
Function to wait:
public static void sleepUntil(long date) throws InterruptedException {
//Thread.sleep(Math.max(date - System.currentTimeMillis(),0));
TimeUnit.MILLISECONDS.sleep(Math.max(date-System.currentTimeMillis(),0));
You should not use a thread on the main thread(UI thread).
You are getting this error because service runs on main thread and you are trying to pause main thread using sleep, try implementing your logic using this and you can use SystemClock.sleep(millis)
for wait
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With