Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add actionListener to ScheduledExecutorService?

 
Ayden Kim
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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?

 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Great Thanks~
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic