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 8 of 8

Thread: time a program

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default time a program

    Hi.
    How can I time my while loop to executeevery 3 seconds?

    Here is my code:
    class Info{			
    			public String name;
    			public String version;
    			public String arch;
    			double CPUSpeed;
    		};
    		Info info =  new Info();
    		Info[] queue = new Info[100];
     
    		int pos = 0;
     
    		for(int i=0;i<100;++i)
    		  queue[i]  = new Info();
     
    		Date d = new Date();
    		long start = d.getTime();
     
    		System.out.println("The starting time is "+ start);
    		while(true){
    			long time = d.getTime();
    			if( (start-time)%3 == 0 )
     
    				System.out.println("The current time is "+ time);
    /*				queue[pos].name = System.getProperty("os.name");
    				System.out.println( queue[pos].name);
    				queue[pos].version = System.getProperty("os.version");
    				queue[pos].arch = System.getProperty("os.arch");
    				pos++;
    */
    		}
    I seem to have done it right, but it doesn't work...
    What is wrong?

    Thanks in advance


  2. #2
    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: time a program

    Use a Timer rather than a while( true ) loop.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: time a program

    Quote Originally Posted by GregBrannon View Post
    Use a Timer rather than a while( true ) loop.
    How?Timer is a cless or what?

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: time a program

    Your code does not work at all.
    If you create a Date object it will not start counting up. It will always return the same time when you call the getTime() method.

    There is 2 ways you can do it, either you try to implement this yourself (which is always nice if you want to learn something) or you use the standard classes which give you this functionality.
    One of the standard classes you can use is the Timer class from Swing. Read the API for this class if you want more information:
    Timer (Java Platform SE 7 )
    or
    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)


    If you want to do it yourself you should use System.nanoTime(); to get the current time in nanoseconds.
    In between checks you should call Thread.yield(); to make the current thread sleep for a short period of time, or Thread.sleep(int millis); to make the thread sleep for a specified amount of time.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: time a program

    Quote Originally Posted by Cornix View Post
    Your code does not work at all.
    If you create a Date object it will not start counting up. It will always return the same time when you call the getTime() method.

    There is 2 ways you can do it, either you try to implement this yourself (which is always nice if you want to learn something) or you use the standard classes which give you this functionality.
    One of the standard classes you can use is the Timer class from Swing. Read the API for this class if you want more information:
    Timer (Java Platform SE 7 )
    or
    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)


    If you want to do it yourself you should use System.nanoTime(); to get the current time in nanoseconds.
    In between checks you should call Thread.yield(); to make the current thread sleep for a short period of time, or Thread.sleep(int millis); to make the thread sleep for a specified amount of time.
    In between what checks?

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: time a program

    If you check for how much time has passed.
    Since a simple if is incredibly fast you would check thousands of times per second, thats very inefficient use of CPU cycles.
    After you checked how much time has passed call yield or sleep to prevent busy waiting.

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: time a program

    I used TimeUnit.
    But now I want to specify a period, say executing function A() for 5 minutes and then execute function B()

  8. #8
    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: time a program

    Have method A keep track of how long it can execute and when the time is up, method A should exit so that method B can execute.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. TIME PROGRAM UNSOLVED
    By memyselfandi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 9th, 2014, 04:04 PM
  2. Need Help With First Time Program
    By UnrealOl1ve in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 1st, 2014, 08:03 AM
  3. [SOLVED] How much time should I have on a program?
    By SOG in forum Java Theory & Questions
    Replies: 17
    Last Post: July 19th, 2011, 05:28 PM
  4. please help me with this program, I dont have enough time to do it
    By n.azizabadi in forum Member Introductions
    Replies: 1
    Last Post: June 10th, 2011, 03:35 AM
  5. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM