Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36664
| Path | csiph.com!aioe.org!.POSTED!not-for-mail |
|---|---|
| From | newsgroups <pippo.pluto@disney.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | timer task(s) with parameters |
| Date | Tue, 30 May 2017 13:35:33 +0200 |
| Organization | Aioe.org NNTP Server |
| Lines | 68 |
| Message-ID | <ogjle5$1fq$1@gioia.aioe.org> (permalink) |
| NNTP-Posting-Host | EPoZP2+NnENyz4kAY/s5CA.user.gioia.aioe.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Complaints-To | abuse@aioe.org |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 |
| X-Notice | Filtered by postfilter v. 0.8.2 |
| X-Mozilla-News-Host | news://news.mozilla.org:119 |
| Content-Language | en-US |
| Xref | csiph.com comp.lang.java.programmer:36664 |
Show key headers only | 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
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