/* * 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 ThreadsHelloWorldSyncSetGet_Set implements Runnable { private ThreadsHelloWorldSyncSetGet_HoldValue objHoldValue; public ThreadsHelloWorldSyncSetGet_Set( ThreadsHelloWorldSyncSetGet_HoldValue objHoldValue ) { this.objHoldValue = objHoldValue; } public void run() { int intLoopCount = 5; String strMsg = ""; String strThreadName = Thread.currentThread().getName(); System.out.println(strThreadName + " - Start Time - " + (new Date()).toString()); for (int i = 1; i <= intLoopCount; i++) { try { int intSleep = ( (int) Math.round((Math.random() * 3)) ); System.out.println( strThreadName + " - Sleep seconds = " + intSleep + " - " + (new Date()).toString()); Thread.sleep( intSleep * 1000 ); //Sleep for 0 to 3 seconds. } catch (InterruptedException e) { //Do nothing. } strMsg = "Hello World #" + i ; System.out.println( strThreadName + " - Set value! - " + strMsg + " - " + (new Date()).toString()); objHoldValue.setSyncValue( strMsg ); } System.out.println(strThreadName + " - Stop Time - " + (new Date()).toString()); } }