// // example of threads in Java, showing use of interrupt(), suspend(), // resume() // // see main method below for command-line arguments. // import java.io.IOException ; // // a TickThread prints "tick .. tick .." etc., until interrupted // by an InputThread // class TickThread extends Thread { boolean cont = true ; // method to allow InputThread to set cont void setContinue(boolean c) { cont = c ; } public void run() { System.out.println("TickThread starting") ; suspend() ; // resume() will be done by InputThread // loop until InputThread sets cont to false while (cont) { try { System.out.print("tick .. ") ; System.out.flush() ; sleep(1000l) ; } catch (InterruptedException e) { // InterruptedException is thrown // if we're interrupted during sleep; // exception clears interrupt, so // resignal for test below interrupt() ; } if (interrupted()) { // if InputThread interrupted us, // suspend; resume() will be done by // InputThread suspend() ; } } System.out.println("TickThread ending") ; } } // // an InputThread controls the execution of a TickThread: // prompts the user to press enter for pause // allows TickThread to resume // when enter is pressed, interrupts TickThread and asks user // whether to continue // sets own and TickThread's "continue" flags (cont) based on // response // repeats until cont = false // class InputThread extends Thread { TickThread ticker ; InputThread(TickThread t1) { ticker = t1 ; } public void run() { boolean cont = true ; int inchar ; try { while (cont) { // prompt user and revive suspended TickThread System.out.println() ; System.out.println("Press enter key to pause") ; ticker.resume() ; // wait for user input; when received, // interrupt TickThread and ask user whether // to continue inchar = System.in.read() ; flushInput() ; ticker.interrupt() ; System.out.println() ; System.out.println("Continue? (y or n)") ; inchar = System.in.read() ; flushInput() ; // set own and TickThread's "continue" flags cont = ((char) inchar == 'y') ; ticker.setContinue(cont) ; } } catch (IOException e) { System.out.println("I/O exception?!") ; } // revive TickThread one last time to let it finish up ticker.resume() ; } // discards waiting input void flushInput() throws IOException { // throw away waiting input for (int i = 0 ; i < System.in.available() ; i++) { int inchar = System.in.read() ; } } } public class TestInterrupt { // command-line arguments are integers P1, P2. // creates and starts a TickThread with priority P1 and an // InputThread with priority P2. // observe the differences in behavior when P1 = P2, P1 < P2, // P1 > P2. for Unix, only P1 > P2 seems to give proper // behavior. for Windows, all choices seem to give proper // behavior sometimes, but P1 > P2 sometimes doesn't work. public static void main(String[] args) { if (args.length < 2) { System.out.println("Arguments are:") ; System.out.println(" priority for ticker") ; System.out.println(" priority for inputter") ; System.exit(-1) ; } int tprty = Integer.parseInt(args[0]) ; int iprty = Integer.parseInt(args[1]) ; TickThread ticker = new TickThread() ; InputThread inputter = new InputThread(ticker) ; ticker.setPriority(tprty) ; inputter.setPriority(iprty) ; ticker.start() ; inputter.start() ; } }