I am fairly new to Java; (<= lol I just typed semicolon instead of dot (after 2 days of non-stop coding. Leaving it there so that everyone can laugh as well :D )
I wasn't sure what I was looking for, so the title is not very helpful.
Best I can describe is that things seems to run parallel. I am not sure how to explain this, so I'll show you an example. I have a timer class that waits for a duration each loop.
If I run the following method(not in the timer class, but just another one), it doesn't wait until the Runnable part(Timer) is finished like for loop would do. It computes the code below at the same time.
How can I make the program to wait for the Timer to finish? Also, why is it running parallel?
I trimmed most of the unnecessary codes but please ask if you need the whole code.
t = Timer;
public void turnend() {
if (leftover == 0) {
housenumber = 1;
Runnable r = new Runnable() {
@Override
public void run() {
//code1
if (housenumber == 8) {
t.stopTimer();
}
}
};
t = new Timer(r, 500, true);
}
//code 2
}
//code 1 and 2 runs at the same time
EDIT: Timer class
import android.os.Handler;
public class Timer {
private Handler handler;
private boolean paused;
private int interval;
private Runnable task = new Runnable () {
@Override
public void run() {
if (!paused) {
runnable.run ();
Timer.this.handler.postDelayed (this, interval);
}
}
};
private Runnable runnable;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public void startTimer () {
paused = false;
handler.postDelayed (task, interval);
}
public void stopTimer () {
paused = true;
}
public Timer (Runnable runnable, int interval, boolean started) {
handler = new Handler ();
this.runnable = runnable;
this.interval = interval;
if (started)
startTimer ();
}
}