The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » SwingLabs

Thread: JSR296 - Scheduled background tasks ?

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 1 - Last Post: Apr 25, 2007 5:30 PM by: Hans Muller
fproulx

Posts: 8
JSR296 - Scheduled background tasks ?
Posted: Apr 25, 2007 4:11 PM
  Click to reply to this thread Reply

Hi.

I'm not sure I should post this here, but I have a question regarding the JSR296 API.

I currently do not think it is possible use the current API to schedule periodic background tasks (kinda like Timer / TimerTask does). I sincerely think it would be important to have that in JSR296.

Maybe it does not fit the concept of JSR296, but I'd like to hear your thoughts and/or suggestions on how to do it. I really wish I could use the same facilities in TaskMonitor to handle message passing etc... from my background tasks that are run periodically.

Hans Muller
Re: JSR296 - Scheduled background tasks ?
Posted: Apr 25, 2007 5:30 PM   in response to: fproulx
  Click to reply to this thread Reply

jdnc-interest@javadesktop.org wrote:
> Hi.
>
> I'm not sure I should post this here, but I have a question regarding the JSR296 API.
>
> I currently do not think it is possible use the current API to schedule periodic background tasks (kinda like Timer / TimerTask does). I sincerely think it would be important to have that in JSR296.
>
> Maybe it does not fit the concept of JSR296, but I'd like to hear your thoughts and/or suggestions on how to do it. I really wish I could use the same facilities in TaskMonitor to handle message passing etc... from my background tasks that are run periodically.
> [Message sent by forum member 'fproulx' (fproulx)]
>
> http://forums.java.net/jive/thread.jspa?messageID=214359
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>

It's easy enough to create a Task that does some work periodically. The
example below does some work on the EDT periodically.

Creating a more elaborate API that allows one to schedule Tasks to run
at absolute/relative times as well as supporting Tasks that are created
and run periodically, is certainly possible. It's not part of the plan
for the first Application Framework release.

- Hans

----------

import application.ApplicationContext;
import application.SingleFrameApplication;
import application.Task;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.JLabel;


/**
* Demonstrates creating a Task that runs periodically. The
* Tasks's {@code process} method runs on the EDT,
* every period milliseconds.
*
* @author Hans Muller (Hans.Muller@Sun.COM)
*/
public class PeriodicTaskExample extends SingleFrameApplication {
JLabel label = null;

class MyPeriodicTask extends Task<Void, Void> {
private final long period;
MyPeriodicTask(long period) {
this.period = period;
}
public Void doInBackground() throws InterruptedException {
while(!isCancelled()) {
Thread.sleep(period);
publish((Void)null);
}
return (Void)null;
}
public void process(List<Void> ignored) {
long dt = getExecutionDuration(TimeUnit.MILLISECONDS);
label.setText("Elapsed time: " + dt);
}
}

@Override protected void startup() {
label = new JLabel("Ready...");
show(label);
}

@Override protected void ready() {
ApplicationContext ac = ApplicationContext.getInstance();
ac.getTaskService().execute(new MyPeriodicTask(500L));
}

public static void main(String[] args) {
launch(PeriodicTaskExample.class, args);
}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net





 XML java.net RSS