Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36664

timer task(s) with parameters

From newsgroups <pippo.pluto@disney.com>
Newsgroups comp.lang.java.programmer
Subject timer task(s) with parameters
Date 2017-05-30 13:35 +0200
Organization Aioe.org NNTP Server
Message-ID <ogjle5$1fq$1@gioia.aioe.org> (permalink)

Show all headers | View raw


I want to schedule a timer task twice:
once at currentime+15 secs with parameter="first" (and then every 22
secs, but that's irrelevant)
then at currentime+15+15 secs with parameter="second".
Unfortunately, all the scheduled task run with parameter="second"
:-(
What am I doing wrong ?
Thank for your help !


Standalone compilable/runnable code:
(watch le line wrap)

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class SchedulaAllarme extends TimerTask {
    private static String messageToShow;
    SchedulaAllarme(String dataToShow) {
       this.messageToShow = dataToShow;
    }
    public void run() {
       System.out.println("---->run @ " + new Date() + " ->" +
messageToShow);
    }
    public static void main(String[] args) throws ParseException {
       String clock, message;
       DateFormat format1;
       Timer timer;
       Calendar calendar;
       format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

       calendar = Calendar.getInstance();
       calendar.add(Calendar.SECOND, 15);
       clock = format1.format(calendar.getTime());

       timer = new Timer();
       message = "first";
       timer.schedule(new SchedulaAllarme(message), new Date(clock), 22 *
1000);
       System.out.println("1 scheduled for:" + clock + " [" + message + 
"]");

       calendar.add(Calendar.SECOND, 15);
       clock = format1.format(calendar.getTime());

       timer = new Timer();
       message = "second";
       timer.schedule(new SchedulaAllarme(message), new Date(clock), 22 *
1000);
       System.out.println("2 scheduled for:" + clock + " [" + message + 
"]");
    }
}

Output of the execution:

1 scheduled for:2017/05/30 13:23:54 [first]
2 scheduled for:2017/05/30 13:24:09 [second]
---->run @ Tue May 30 13:23:54 CEST 2017 ->second
---->run @ Tue May 30 13:24:09 CEST 2017 ->second
---->run @ Tue May 30 13:24:16 CEST 2017 ->second

...and so on

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

timer task(s) with parameters newsgroups <pippo.pluto@disney.com> - 2017-05-30 13:35 +0200
  Re: timer task(s) with parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-05-30 07:57 -0400
    Re: timer task(s) with parameters newsgroups <pippo.pluto@disney.com> - 2017-05-30 14:05 +0200
    Re: timer task(s) with parameters Robert Klemme <shortcutter@googlemail.com> - 2017-05-31 07:50 +0200

csiph-web