/* * 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 * @version 1.0 07/04/08 */ package firststeps; import java.util.Date; public class ThreadsHelloWorldInterruptSimple implements Runnable { public void run() { //The Thread's run() method. int intLoopCount = 5; String strThreadName = Thread.currentThread().getName(); System.out.println(strThreadName + " - Start Time - " + (new Date()).toString()); for (int i = 1; i <= intLoopCount; i++) { //Pause for 2 seconds try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { System.out.println(strThreadName + " - Interrupted Time - " + (new Date()).toString()); if ( strThreadName.equals("ThreadHW2") ) { System.out.println(strThreadName + " - I'm to busy having fun to break! - " + (new Date()).toString()); } else { break; //return; //we could return but you wouldn't see the Stop message in the console. } } System.out.println( strThreadName + " - Hello World #" + i + " - " + (new Date()).toString()); } System.out.println(strThreadName + " - Stop Time - " + (new Date()).toString()); } public static void main(String args[]) throws InterruptedException { String strThreadName = Thread.currentThread().getName(); System.out.println(strThreadName + " - Begin - " + (new Date()).toString()); //First Thread ThreadsHelloWorldInterruptSimple objRunnable1 = new ThreadsHelloWorldInterruptSimple(); Thread objThreadHW1 = new Thread(objRunnable1, "ThreadHW1"); //A thread is not alive until the .start() method is run. System.out.println(strThreadName + " - before objThreadHW1.start() - objThreadHW1.isAlive() = " + objThreadHW1.isAlive()); objThreadHW1.start(); System.out.println(strThreadName + " - before objThreadHW1.start() - objThreadHW1.isAlive() = " + objThreadHW1.isAlive()); System.out.println(strThreadName + " - Before 2 second sleep (Sleep #1) - " + (new Date()).toString()); Thread.sleep(2 * 1000); System.out.println(strThreadName + " - After 2 second sleep (Sleep #1) - " + (new Date()).toString()); //Second Thread ThreadsHelloWorldInterruptSimple objRunnable2 = new ThreadsHelloWorldInterruptSimple(); Thread objThreadHW2 = new Thread(objRunnable2, "ThreadHW2"); objThreadHW2.start(); System.out.println(strThreadName + " - Before 4 second sleep (Sleep #2) - " + (new Date()).toString()); Thread.sleep(4 * 1000); System.out.println(strThreadName + " - After 4 second sleep (Sleep #2) - " + (new Date()).toString()); System.out.println(strThreadName + " - Before objThreadHW1.interrupt() - objThreadHW1.isAlive() = " + objThreadHW1.isAlive()); objThreadHW1.interrupt(); System.out.println(strThreadName + " - After objThreadHW1.interrupt() - objThreadHW1.isAlive() = " + objThreadHW1.isAlive()); //ThreadHW2 gets interrupted but it decides to continue processing. objThreadHW2.interrupt(); System.out.println(strThreadName + " - After objThreadHW2.interrupt() - objThreadHW2.isAlive() = " + objThreadHW2.isAlive()); //Third Thread ThreadsHelloWorldInterruptSimple objRunnable3 = new ThreadsHelloWorldInterruptSimple(); Thread objThreadHW3 = new Thread(objRunnable3, "ThreadHW3"); objThreadHW3.start(); System.out.println(strThreadName + " - Finished - " + (new Date()).toString()); //If you look at the System console you will notice that the threads continue to //run even though the main() has finished. } }