// PlaytimeModified.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-06-21 File created. // 2005-06-21 Last modification. // WARNING! It may be better not to run this program // for a long time on your computer because the infinite // loop of the program consumes quite a lot of processing // power, and that may result in that the electronics // of your computer gets too hot. In my computer the cooling // fan of the processor starts operating immediately when // I start executing this program. import java.util.* ; class ThreadToDisplayTicksAndTime extends Thread { boolean must_display_ticks_and_time = true ; public void stop_this_thread() { must_display_ticks_and_time = false ; } public void run() { long previous_time_ticks, current_time_ticks ; long loop_counter = 0 ; System.out.print( "\n nanoseconds current_time_ticks Readable time \n" ) ; previous_time_ticks = System.currentTimeMillis() ; while ( must_display_ticks_and_time == true ) { loop_counter ++ ; current_time_ticks = System.currentTimeMillis() ; if ( ( current_time_ticks - previous_time_ticks ) > 5000 && ( ( current_time_ticks / 1000 ) % 5 ) == 0 ) { double time_to_execute_the_loop_once = ( ( (double) ( current_time_ticks - previous_time_ticks ) ) * 1000000.0 ) / (double) loop_counter ; // %12.4 says that the number is shown with 4 decimals. System.out.printf( "\n %12.4f", time_to_execute_the_loop_once ); System.out.print( " " + current_time_ticks + " " ) ; System.out.printf( "%tc", current_time_ticks ) ; previous_time_ticks = current_time_ticks ; loop_counter = 0 ; } } } } class PlaytimeModified { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; ThreadToDisplayTicksAndTime thread_to_display_ticks_and_time = new ThreadToDisplayTicksAndTime() ; System.out.print( "\n Press the Enter key to stop the program.\n" ) ; thread_to_display_ticks_and_time.start() ; String any_string_from_keyboard = keyboard.nextLine() ; thread_to_display_ticks_and_time.stop_this_thread() ; } }