1

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

Lain
  • 2,166
  • 4
  • 23
  • 47
user2263104
  • 99
  • 1
  • 4
  • 11
  • possible duplicate of [How to call a method after a delay](http://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay) – Aubin Apr 09 '13 at 18:48
  • When you say it doesn't exit from the thread do you mean that the pool thread stays running? I assume the `run()` method completes, right? – Gray Apr 09 '13 at 18:52
  • Yes. That i think that is what the problem is. The pool thread remains running. The run() method does complete, but the thread continues to run. – user2263104 Apr 09 '13 at 19:00
  • @Aubin, i have tried the solutions in the link you specified. But none of them seemed to work. – user2263104 Apr 09 '13 at 19:02
  • The pool thread will be shutdown if you call `exec.shutdown();`. Something else is going on. Maybe do a thread dump using kill -QUIT to see what threads are running? If you still see the `run()` method executing then it is blocked on something. – Gray Apr 09 '13 at 19:05
  • What is wrong with `Thread.sleep()`? – Ralf H Apr 09 '13 at 21:02

3 Answers3

1

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.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • I have tried exec.shutdown() as well. But the control still isn't going back to the first thread and remains with the thread created by the ScheduledPoolThreadExecutor. – user2263104 Apr 09 '13 at 19:00
  • What do you mean by the "control"? Has the `run()` method finished or is it blocked doing UI operations or something? – Gray Apr 09 '13 at 19:01
  • The UI freezes after the exampleFunction() runs. So i was under the impression that execution is still in the ScheduledPoolThreadExecutor thread and doesn't go back to the UI. – user2263104 Apr 09 '13 at 19:58
1

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.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
0

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.

user2246674
  • 7,621
  • 25
  • 28
  • How do i make sure i am using it as a pooled executor? Do i have to do something different from the code above? I do not wish to block the current thread. I want the current thread to execute normally, and then i want to run exampleFunction() on the current thread after 10 seconds. – user2263104 Apr 09 '13 at 18:53