Reputation: 134
I am programming an EBookReaderApp for Android. It has an automatically scrolling function. The scrolling is implemented in a separate thread to not block the UI all the time. The actual scroll has to run on the UI thread. Therefore I call Activity.runOnUiThread(Runnable) So far so good. After some time (sometime as low as 2 minutes, sometimes after 1 hour) the Runnable stops being executed. Has somebody an idea why runOnUiThread stops executing things completely?
private class ScrollThread implements Runnable
{
@Override
public void run()
{
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
boolean initialised = false;
long lastCall = System.currentTimeMillis()-25;
Runnable r = new Runnable()
{
public void run()
{
log("b");
content.scrollTo(0, (int)scrollValue);
}
};
while (!kill)
{
long diffTime = System.currentTimeMillis()-lastCall;
lastCall = System.currentTimeMillis();
float fps = 1000f/diffTime;
if (!initialised)
{
if (initialisedChapters == chapters.size())
{
if (content.getHeight() > 0)
{
initialised = true;
}
}
}
if (initialised)
{
if (!scrollBreak)
{
scrollValue += content.getHeight()/fps/secondsPerPage;
}
recalculateChapter();
log("a");
runOnUiThread(r);
}
try
{
diffTime = System.currentTimeMillis()-lastCall;
long sleepTime = 1000/40-diffTime;
if (sleepTime > 0)
{
Thread.sleep(sleepTime);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
logfile:
2022-01-20T14:35:32.109+0100 a
2022-01-20T14:35:32.115+0100 b
2022-01-20T14:35:32.145+0100 a
2022-01-20T14:35:32.151+0100 b
2022-01-20T14:35:32.179+0100 a
2022-01-20T14:35:32.185+0100 b
2022-01-20T14:35:32.210+0100 a
2022-01-20T14:35:32.218+0100 b
2022-01-20T14:35:32.246+0100 a
2022-01-20T14:35:32.254+0100 b
2022-01-20T14:35:32.276+0100 a
2022-01-20T14:35:32.284+0100 b
2022-01-20T14:35:32.309+0100 a
2022-01-20T14:35:32.330+0100 b
2022-01-20T14:35:32.344+0100 a
2022-01-20T14:35:32.348+0100 b
[...]
2022-01-20T14:55:39.440+0100 a
2022-01-20T14:55:39.447+0100 b
2022-01-20T14:55:39.475+0100 a
2022-01-20T14:55:39.482+0100 b
2022-01-20T14:55:39.510+0100 a
2022-01-20T14:55:39.515+0100 b
2022-01-20T14:55:39.545+0100 a
2022-01-20T14:55:39.580+0100 a
2022-01-20T14:55:39.616+0100 a
2022-01-20T14:55:39.651+0100 a
2022-01-20T14:55:39.678+0100 a
2022-01-20T14:55:39.713+0100 a
2022-01-20T14:55:39.746+0100 a
2022-01-20T14:55:39.781+0100 a
It keeps printing "a", "b" is never printed again... I am absolutly clueless and did not find solutions.
Upvotes: 1
Views: 163