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


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

timer task(s) with parameters

Started bynewsgroups <pippo.pluto@disney.com>
First post2017-05-30 13:35 +0200
Last post2017-05-31 07:50 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#36664 — timer task(s) with parameters

Fromnewsgroups <pippo.pluto@disney.com>
Date2017-05-30 13:35 +0200
Subjecttimer task(s) with parameters
Message-ID<ogjle5$1fq$1@gioia.aioe.org>
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

[toc] | [next] | [standalone]


#36665

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-05-30 07:57 -0400
Message-ID<ogjmg5$49b$1@dont-email.me>
In reply to#36664
On 5/30/2017 7:35 AM, newsgroups wrote:
> 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 ?
> [...]
> 
> public class SchedulaAllarme extends TimerTask {
>     private static String messageToShow;

     Because messageToShow is `static', it belongs to the entire
class as a whole.  There is only one messageToShow, no matter how
many SchedulaaAllarme objects you create; they all share the same
messageToShow value.  So ...

>     SchedulaAllarme(String dataToShow) {
>        this.messageToShow = dataToShow;

     ... the first time this constructor runs, it sets the shared
messageToShow to the dataToShow value.  And the second time the
constructor runs, it changes the shared messageToShow to *its*
dataToShow value.

     To give every SchedulaAllarme object its own unique messageToShow,
remove `static' from the declaration.

     (Note: I have not examined the rest of your code carefully;
there might be other problems, too.)

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred thirty-one days to go.

[toc] | [prev] | [next] | [standalone]


#36666

Fromnewsgroups <pippo.pluto@disney.com>
Date2017-05-30 14:05 +0200
Message-ID<ogjn5k$4gl$1@gioia.aioe.org>
In reply to#36665
On 05/30/2017 01:57 PM, Eric Sosman wrote:
>      Because messageToShow is `static', it belongs to the entire
> class as a whole.  There is only one messageToShow, no matter how
> many SchedulaaAllarme objects you create; they all share the same
> messageToShow value.  So ...
BINGO !
Thank you !
1 scheduled for:2017/05/30 14:03:20 [first]
2 scheduled for:2017/05/30 14:03:35 [second]
---->run @ Tue May 30 14:03:20 CEST 2017 ->first
---->run @ Tue May 30 14:03:35 CEST 2017 ->second
---->run @ Tue May 30 14:03:42 CEST 2017 ->first
---->run @ Tue May 30 14:03:57 CEST 2017 ->second

[toc] | [prev] | [next] | [standalone]


#36667

FromRobert Klemme <shortcutter@googlemail.com>
Date2017-05-31 07:50 +0200
Message-ID<ep73u0FnegkU1@mid.individual.net>
In reply to#36665
On 30.05.2017 13:57, Eric Sosman wrote:

>      (Note: I have not examined the rest of your code carefully;
> there might be other problems, too.)

A few observations:

For both recurring tasks one Timer instance is created.  Normally the 
approach would be to just use one Timer instance - especially since 
tasks do not seem to be very heavy.

Your use of variable declarations is very C-ish.  Don't do that in Java. 
  Rather declare them for the smallest necessary scope.  This helps 
avoid one class of errors.

format1 can be made final, as well as calendar and timer (when using one 
Timer only).

Even for such a simple example you could factor out the common part of 
both scheduling activities and thus make it more readable.  Getting into 
that habit will help you create more modular and less redundant code.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web