I have a question regarding calling methods after a certain amount of delay.
I want to call a Java method exampleFunction()
after a delay of about 10 seconds. I have looked for solutions online and have come across the ScheduledThreadPoolExecutor()
. So I have used this, but the thing is, once the function runs after 10 seconds, it doesn't exit from the thread. Is there any way I can exit from the thread? Or can I run the ScheduledThreadPoolExecutor()
on the current thread instead of creating a new thread?
class Test {
...
exampleFunction();
...
public void exampleFunction() {
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.schedule(new Runnable() {
public void run() {
...do something here...
}
}, 10, TimeUnit.SECONDS);
}
}
So is there any way I can exit this thread after exampleFunction runs after a delay of 10 seconds? Or can I have the ScheduledThreadPoolExecutor
use the current thread instead of creating a new one?
Or is there another way I can approach this problem? I want to be able to run exampleFunction()
after 10 seconds on the current thread, instead of creating a new thread.
Edit: I think it may not be a thread issue. I'm still trying to figure out the problem is. Thanks everyone for your suggestions and advice.
EDIT: Can I pass an argument to exampleFunction() and then use it inside public void run()?