• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How to add actionListener to ScheduledExecutorService?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. I'm trying to change my code(Timer class -> ScheduledExecutorService class) because of performance issue.

I used Timer class like this.

private Timer makeTimer(int initialDelay, ArrayList<ActionListener> actions) {
.................
.................
final Timer res = new Timer(initialDelay, temp);
if (actions!=null) {
for (ActionListener al : actions)
res.addActionListener(al);
}


But, as you know, there is no addActionListener method in ScheduledExecutorService.

It just use runnable class.

Is there any way to add actionListener to ScheduledExecutorService?

 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Initially, I did not realize that you are mentioning about the Swing Timer class )

If the Action listener is invoked every time the task is run, then you can override the beforeExecute() or afterExecute() methods of the ScheduledThreadPoolExecutor and call these listeners appropriately. Since, you are extending the class, you can also add methods to register your listeners.
 
Ayden Kim
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply.

I'm sorry but can you give me an example how to use beforeExecute() or afterExecute()??

The ArrayList<ActionListener> actions is come from another java file.

Could you give me an more concerete example???

res.addActionListener(al);

Using this method in ScheduledExecutorService class is key fact.

I'm really appreciate your reply.

Thanks.

 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code should give you an idea about what I was referring to. Instead of using the ScheduledThreadPoolExecutor you will use this executor.

 
Ayden Kim
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Thanks~
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of the fact that the afterExecute() is called in the same thread that would have done the task. Doing long running tasks in the listeners in this case may make your threadpool performance suffer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic