Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default How to perform an action only if a certain amount of time has passed

    public static void main(String[] args)
    {
    boolean t=false;

    long cuTime = System.currentTimeMillis();

    while(t==false)
    {
    System.out.println(cuTime);
    long g=cuTime+2000;

    long ct2=System.currentTimeMillis();

    if(ct2>=g)
    {
    System.out.println(ct2-cuTime);
    //action here
    t=true;
    }
    }

    I tried this and it includes a while loop as the whole program has to wait until this while loop executes. So the entire program slows down. Is there any way to do this without a while loop
    Last edited by silvercats; 09-18-2014 at 12:26 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to perform an action only if a certain amount of time has passed

    Check out the java.util.Timer class and its TimerTask companion.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to perform an action only if a certain amount of time has passed

    Those are the better solutions.

    There is also Thread.sleep() which would plugin into the code as presented. Is this for a game loop perhaps?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: How to perform an action only if a certain amount of time has passed

    No it is not a game loop. A class gets a rapidly changing integer from another class. As it changes so fast, I wanted to perform an action only when this number is stabilized. Like if the method receives the same integer value during 3 seconds, then perform an action, otherwise ignore it as it changes too fast. Can someone tell me how to do it? best way

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to perform an action only if a certain amount of time has passed

    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to perform an action only if a certain amount of time has passed

    Using TimerTask as already stated is probably the best way. Another way might be to reverse the situation
    and have the class that generates the number, "signal" the class that needs it after the number has stabilized.
    I presume you are doing other work while the number if being generated. Otherwise, you could just do a
    Thread.sleep() (again, having already been suggested) until the number has been stabilized.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: How to perform an action only if a certain amount of time has passed

    Java Code:
    public class TestClass {
        public long myLong = 3000;
     
        public static void main(String[] args) {
           MyClass test = new MyClass();
     
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
     
                @Override
                public void run() {
                    test.doStuff();
                }
            }, 0, test.myLong);
        }
     
        public void doStuff(){
     
        }
    }
    We could do this with the Timer. But this just repeats an action over a specific interval. This is not what I really want.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to perform an action only if a certain amount of time has passed

    this just repeats an action
    A Timer can be set to only execute once. It does not need to repeat.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to perform an action only if a certain amount of time has passed

    Quote Originally Posted by silvercats View Post
    We could do this with the Timer. But this just repeats an action over a specific interval. This is not what I really want.
    Only because that's the way your are using it. You can use it like a "one shot." Invoke the timer to start the task to the generate
    the number. Once the timer expires it will not repeat until restarted. Or once you are satisfied the number is stable, then stop the timer.
    You could also have the task simply generate numbers and when the number is stable within a certain time period, set a boolean which you
    can poll. There are a variety of ways to approach this (of course, they are all based on my understanding of what you are trying to accomplish).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: How to perform an action only if a certain amount of time has passed

    It would be helpful if a sample code can be provided?

    What I am trying to achieve is

    int x=3

    if(int x=3 "for 3 seconds")
    {
    //action here
    }

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to perform an action only if a certain amount of time has passed

    Quote Originally Posted by silvercats View Post
    What I am trying to achieve is

    int x=3

    if(int x=3 "for 3 seconds")
    {
    //action here
    }
    Assuming multiple threads, what if x is set to 3? Then you wait three seconds and during that time x has changed many times
    to many different values. Then when you check it, x == 3 again? Do consider that stabilized? I guess I just don't fully understand
    how you want to do this or what the behavior of your code is that is changing x.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to perform an action only if a certain amount of time has passed

    Would that require polling (checking the value at short intervals) to determine if the value had not changed for 3 seconds?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to perform an action only if a certain amount of time has passed

    Java Forums at the Big Moose Saloon
    (may not be visible to all)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: How to perform an action only if a certain amount of time has passed

    Can't someone post in several forums ?

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to perform an action only if a certain amount of time has passed

    You can, but of course post links to your duplicate threads. Otherwise someone may invest their personal / working time into answering you when the answer has already been given somewhere else.

    Asking a question is the act of wasting as much of your own time as you possibly can to ask it, and waste as little time of the people you ask for help to answer it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: How to perform an action only if a certain amount of time has passed

    If I get the correct answer, I will post them in all threads

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to perform an action only if a certain amount of time has passed

    You already got a correct answer. You just don't have a correct understanding yet.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to perform an action only if a certain amount of time has passed

    Have you posted links to all the forums that you have asked this question on so we can see what has been said and not waste time repeating it?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: How to perform an action only if a certain amount of time has passed

    Quote Originally Posted by Norm View Post
    Would that require polling (checking the value at short intervals) to determine if the value had not changed for 3 seconds?
    yup that is it.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to perform an action only if a certain amount of time has passed

    Quote Originally Posted by silvercats View Post
    yup that is it.
    No it isn't; think of it this way: the first moment a wanted target value arrives, you start that TimerTask for a certain time in the future; next you do nothing until a next number comes in. If the number comes in later than some threshold time, the Timer has fired and the TimerTask has run already. Otherwise, check the incoming number; if it is the target value, don't do anything, otherwise cancel the TimerTask and create a new one when a correct target value comes in. This takes more words than code ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 14
    Last Post: 06-27-2014, 09:16 PM
  2. Replies: 2
    Last Post: 02-17-2011, 12:07 PM
  3. My buttons all perform their action like 100 times
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 10:49 AM
  4. Perform one action at a time
    By Melki in forum AWT / Swing
    Replies: 6
    Last Post: 12-08-2008, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •