Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

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

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. therefor 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


  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Well i dont know. Maybe a for loop?
    For (blablabla; if blala
    You know what i mean

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    Use a java.util.Timer.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Here is what I want to achieve.

    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
    Last edited by silvercats; September 18th, 2014 at 07:13 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

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

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    yup. Neither got a working answer.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Did you look at using a Timer?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by Norm View Post
    Did you look at using a Timer?
    What I want is,

    int x=3;

    if (x==3 'during past 3 seconds')
    {
    //perform action
    }
    this should not disturb other actions of the program for this 3 secs.

    But Timer lets us only perform a certain action in intervals or count time passed to execute an action.



    Something like this is not what I want exactly. This is all I managed get with the timer

    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(){
     
        }
    }
    Last edited by silvercats; September 18th, 2014 at 08:21 AM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    The OP and Norm are having a great conversation in the other forum (post #5), albeit somewhat circular. I don't see much point in continuing this thread.

    Thread closed.

Similar Threads

  1. Replies: 1
    Last Post: May 29th, 2013, 06:22 AM
  2. Passed array value is null
    By NoobException in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 23rd, 2013, 10:01 PM
  3. No action time out
    By skuskusas in forum Java Theory & Questions
    Replies: 4
    Last Post: November 9th, 2012, 07:29 AM
  4. Replies: 1
    Last Post: July 9th, 2011, 07:17 AM
  5. [SOLVED] loop , passed and failed
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: August 25th, 2009, 07:00 AM