/* * 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 ThreadsHelloWorldWaitInterruptSimple implements Runnable { private String strMsg = "Will need info soon! - "; public String getStrMsg() { return strMsg; } public void setStrMsg(String strMsg) { this.strMsg = strMsg; } 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++) { if ( i == 3 ) { try { System.out.println(strThreadName + " - Let's wait() - " + (new Date()).toString()); synchronized ( this ) { this.wait(); //Thread wants to wait until main thread creates a message for him. } } catch (IllegalMonitorStateException e) { System.out.println(strThreadName + " - Illegal Monitor State Exception (IllegalMonitorStateException) - you can't do that! Try synchronized - " + (new Date()).toString()); } catch (InterruptedException e) { System.out.println(strThreadName + " - Interrupted Time - " + (new Date()).toString()); } } System.out.println( strThreadName + " - Hello World #" + i + " - " + getStrMsg() + (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 ThreadsHelloWorldWaitInterruptSimple objRunnable1 = new ThreadsHelloWorldWaitInterruptSimple(); Thread objThreadHW1 = new Thread(objRunnable1, "ThreadHW1"); objThreadHW1.start(); System.out.println(strThreadName + " - before 2 second sleep - " + (new Date()).toString()); Thread.sleep(2 * 1000); System.out.println(strThreadName + " - after 2 second sleep - " + (new Date()).toString()); objRunnable1.setStrMsg("Got it now! - "); //Create a message for ThreadHW1, then interrupt him. System.out.println(strThreadName + " - objThreadHW1.interrupt() - " + (new Date()).toString()); objThreadHW1.interrupt(); //This let's ThreadHW1 know the message is ready! System.out.println(strThreadName + " - Finished - " + (new Date()).toString()); } }