/* * 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; public class ThreadsHelloWorldSyncSetGet_HoldValue { private String strHoldValue; private boolean blnValueReadyForGet = false; private boolean blnFinished = false; public synchronized String getSyncValue() throws InterruptedException { //If the methods was not "synchronized" at run time a java.lang.IllegalMonitorStateException will be thrown. while (blnValueReadyForGet == false) { try { wait(); } catch (InterruptedException e) { if ( isBlnFinished() ) { throw new InterruptedException(); } } } blnValueReadyForGet = false; notifyAll(); return getStrHoldValue(); } public synchronized void setSyncValue(String strPutValue) { //If the methods was not "synchronized" at run time a java.lang.IllegalMonitorStateException will be thrown. while (blnValueReadyForGet == true) { try { wait(); } catch (InterruptedException e) { } } setStrHoldValue(strPutValue); blnValueReadyForGet = true; notifyAll(); } //Getters and Setters - public. public boolean isBlnFinished() { return blnFinished; } public synchronized void setBlnFinished(boolean blnFinished) { this.blnFinished = blnFinished; } //Getters and Setters - private because we use the sync'ed methods to set & get. private String getStrHoldValue() { return strHoldValue; } private void setStrHoldValue(String strHoldValue) { this.strHoldValue = strHoldValue; } }