// DotsAndDollars_16_9.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-09-14 File created. // 2005-05-15 Last modification. // A solution to exercise 16-9. import java.util.* ; class ThreadToPrintCharacters extends Thread { boolean thread_must_be_executed = true ; char character_to_print ; long interval_between_printings ; ThreadToPrintCharacters( char given_character_to_print, int given_interval_between_printings ) { character_to_print = given_character_to_print ; interval_between_printings = given_interval_between_printings ; } public void stop_this_thread() { thread_must_be_executed = false ; } public void run() { while ( thread_must_be_executed == true ) { try { Thread.sleep( interval_between_printings ) ; } catch ( InterruptedException caught_exception ) { } System.out.print( " " + character_to_print ) ; } } } class DotsAndDollars_16_9 { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; ThreadToPrintCharacters thread_to_print_dots = new ThreadToPrintCharacters( '.', 1000 ) ; ThreadToPrintCharacters thread_to_print_dollar_signs = new ThreadToPrintCharacters( '$', 4050 ) ; thread_to_print_dots.start() ; thread_to_print_dollar_signs.start() ; System.out.print( "\n Press the Enter key to stop the program. \n\n" ) ; String any_string_from_keyboard = keyboard.nextLine() ; thread_to_print_dots.stop_this_thread() ; thread_to_print_dollar_signs.stop_this_thread() ; } }