Path: csiph.com!aioe.org!.POSTED!not-for-mail From: newsgroups 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: 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 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