user2263104
user2263104

Reputation: 109

How do i run a method after a delay?

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()?

Upvotes: 1

Views: 5808

Answers (3)

Tim Bender
Tim Bender

Reputation: 20442

Based on all the comments and confusion, any answer is just a guess.

What I think you want:

  • The UI thread to invoke exampleFunction
  • 'exampleFunction` to schedule a task for 10 seconds later and return immediately
  • In 10 seconds time, to have the run method be invoked on the UI thread

In Swing, this is done by using SwingUtilities.invokeLater.

ExampleFunction would look like this:

 public void exampleFunction() {
     new Thread() {
         public void run() {
             TimeUnit.SECONDS.sleep(10); //Will need a try/catch
             SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                     ...do something here...
                 }
             });
         }
     }.start();
 }

Note: SwingUtilities.invokeAndWait could also be used.
Note 2: Although not usually advised, a simple Thread here is simpler than making a new Thread pool.

Upvotes: 1

Gray
Gray

Reputation: 116828

I believe your problem may be that you are not shutting down the executor after your submit the job to it.

exec.schedule(...);
exec.shutdown();

The jobs that have been submitted will continue to run but you have to shutdown the service after you've submitted the last job to it.

Upvotes: 1

user2246674
user2246674

Reputation: 7709

Thread.sleep can be used if merely wishing to making the current thread block. If you do use a pooled executor, make sure to use it as a pooled executor - not one per (new) thread. To "exit" from a thread, just let execution run out of run. If using Swing, use the EDT.

Upvotes: 0

Related Questions