/* * Copyright (c) Michael Thomas, All Rights Reserved. (michael@michael-thomas.com) * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Exceptions must be in writing between the * copyright holder and entity using the content. * * THE COPYRIGHT HOLDER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE COPYRIGHT HOLDER SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @author Michael Thomas - michael@michael-thomas.com * @version 1.0 07/04/08 */ package firststeps; import java.util.Date; public class ThreadsHelloWorldInterruptCheckSimple { private static class ThreadsInterruptCheckWhileBusy implements Runnable { public void run() { //The Thread's run() method. int intLoopCount = 100; int intLoopCrunchCount = 1000000; int intTotalLoopCount = 0; String strThreadName = Thread.currentThread().getName(); System.out.println(strThreadName + " - Start Time - " + (new Date()).toString()); boolean blnWasInterrupted = false; for (int i = 1; i <= intLoopCount; i++) { //Start loop crunching! for (int x = 1; x <= intLoopCrunchCount; x++) { intTotalLoopCount += 1; if (Thread.interrupted()) { //We've been interrupted. Finish this loop, then exit the main for loop. System.out.println(strThreadName + " - Interrupted Time, but I'm busy so wait! - Completed " + intTotalLoopCount + " loops - " + (new Date()).toString()); System.out.println(strThreadName + " - Hmmmm, the interrupt was reset! Now Thread.interrupted() = " + Thread.interrupted() + " - " + intTotalLoopCount + " loops - " + (new Date()).toString()); blnWasInterrupted = true; //We need this because the interrupt status gets reset. } } System.out.println( strThreadName + " - Hello World #" + i + " - " + (new Date()).toString()); if ( blnWasInterrupted ) { //This is the controlled break point that the loop wants to exit at! System.out.println(strThreadName + " - thread not finished, but will exit here - " + (new Date()).toString()); break; } } System.out.println(strThreadName + " - Stop Time - Completed " + intTotalLoopCount + " loops - " + (new Date()).toString()); } } public static void main(String args[]) throws InterruptedException { String strThreadName = Thread.currentThread().getName(); System.out.println(strThreadName + " - Begin - " + (new Date()).toString()); ThreadsInterruptCheckWhileBusy objRunnable1 = new ThreadsInterruptCheckWhileBusy(); Thread objThreadHW1 = new Thread(objRunnable1, "ThreadHW1"); objThreadHW1.start(); Thread.sleep(2 * 1000); System.out.println(strThreadName + " - before objThreadHW1.interrupt() - " + (new Date()).toString()); objThreadHW1.interrupt(); //Send an interrupt request to the thread. System.out.println(strThreadName + " - after objThreadHW1.interrupt() - " + (new Date()).toString()); System.out.println(strThreadName + " - Finished - " + (new Date()).toString()); } }