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


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

Re: timer task(s) with parameters

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: timer task(s) with parameters
Date Tue, 30 May 2017 07:57:08 -0400
Organization A noiseless patient Spider
Lines 35
Message-ID <ogjmg5$49b$1@dont-email.me> (permalink)
References <ogjle5$1fq$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Tue, 30 May 2017 11:53:41 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="1998bc43012bd3294325666c70d3a3b3"; logging-data="4395"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+nE6VytEufEmQMjK6hk2Wm"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1
In-Reply-To <ogjle5$1fq$1@gioia.aioe.org>
Content-Language en-US
Cancel-Lock sha1:e2Hu0XbNajD35nEQ8Zp3oUncEs0=
Xref csiph.com comp.lang.java.programmer:36665

Show key headers only | View raw


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.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | 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