W
wanwan
I need to have a task repeated periodically in a thread . for now I'm
using inside the run method :
long pausetime = 2000;
long p = System.currentTimeMillis();
long q;
while(true){
if (p < (q = System.currentTimeMillis() - pausetime)) {
p = q + pausetime;
// modify this object's variables
}
}
// do something
I think this approach is bad because the looped if statement requires
too much work as I increase the number of threads.
I can't use sleep() of Timer object because I'm not calling from
outside the thread. Or if it's possible to use them inside the run
method, I don't know how to do it.
Please help.
using inside the run method :
long pausetime = 2000;
long p = System.currentTimeMillis();
long q;
while(true){
if (p < (q = System.currentTimeMillis() - pausetime)) {
p = q + pausetime;
// modify this object's variables
}
}
// do something
I think this approach is bad because the looped if statement requires
too much work as I increase the number of threads.
I can't use sleep() of Timer object because I'm not calling from
outside the thread. Or if it's possible to use them inside the run
method, I don't know how to do it.
Please help.