wood burning stoves*
The moose likes Threads and Synchronization and the fly likes How to add actionListener to ScheduledExecutorService? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to add actionListener to ScheduledExecutorService?" Watch "How to add actionListener to ScheduledExecutorService?" New topic
Author

How to add actionListener to ScheduledExecutorService?

Ayden Kim
Greenhorn

Joined: Feb 10, 2011
Posts: 5
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

Joined: Feb 25, 2007
Posts: 1638

(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.

apigee, a better way to API!
Ayden Kim
Greenhorn

Joined: Feb 10, 2011
Posts: 5
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

Joined: Feb 25, 2007
Posts: 1638

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

Joined: Feb 10, 2011
Posts: 5
Great Thanks~
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add actionListener to ScheduledExecutorService?