import com.abc.pp.stringhandoff.*;
import com.programix.thread.
//This program has multiple threads passing a string to other threads
public class StringHandoffImpl implements StringHandoff
//String to hold the message to be passed
private boolean isShutdown = false;
private String
public StringHandoffImpl() {
//set the message to null to use it as a flag
}
//passing string calls this function
private boolean isReceiving = false;
@Override
public synchronized void pass(String msg, long msTimeout)
TimedOutException,
ShutdownException,
//if
throwIfShutdownOrPassing();
passAndNotify(msg);
long messagesend = System.currentTimeMillis() + msTimeout;
while ( message
if (message == null)
notifyAll();
}
//wqait
if
if (msTimeout == 0L) {
while (message != null)
}
wait(messagewait);
if ( isShutdown ) {
throw new ShutdownException();
}
@Override
public synchronized void pass(String
ShutdownException,
throwIfShutdownOrPassing();
passAndNotify(msg);
while
wait();
}
notifyAll();
}
//Receiving thread's function
wait();
if ( isShutdown ) {
throw new ShutdownException();
}
}
}
@Override
public synchronized String receive(long msTimeout)
TimedOutException,
ShutdownException,
//variable to hold passing message
String receivedMessage;
if (message != null) {
//If message is not null recieve message change flag notify threads
receivedMessage = message;
message = null;
notifyAll();
return receivedMessage;
}
if (msTimeout == 0L) {
while (message == null) {
wait();
}
receivedMessage = message;
message = null;
notifyAll();
return receivedMessage;
}
//wait if message isn't available correct for time
throwIfShutdownOrReceiving();
long
wait(msRemaining);
msRemaining
long messagewait =
receivedMessage = message;
message = null;
notifyAll();
return receivedMessage;
}
throw new
}
//same method as above without the timeout limitation
}
wait(messagewait);
if ( isShutdown ) {
throw new ShutdownException();
}
}
return receiveAndNotify();
}
@Override
public synchronized String
ShutdownException,
throwIfShutdownOrReceiving();
// while
wait();
}
String receivedMessage = message;
while ( message
notifyAll();
wait();
if ( isShutdown ) {
throw new ShutdownException();
}
}
return
}
}
@Override
public synchronized void shutdown() {
message = null;
isReceiving = false;
notifyAll();
}
@Override
public Object getLockObject() {
return this;
}
private synchronized void passAndNotify(String msg) {
if (msg == "orange") {
System.out.println(isReceiving);
}
message = msg;
notifyAll();
}
private synchronized String receiveAndNotify() {
String msg = message;
message = null;
isReceiving = false;
notifyAll();
return msg;
}
private synchronized void throwIfShutdownOrReceiving() throws ShutdownException, IllegalStateException {
if ( isShutdown ) {
throw new ShutdownException();
}
if ( isReceiving ) {
throw new IllegalStateException();
}
isReceiving = true;
}
private synchronized void throwIfShutdownOrPassing() throws ShutdownException, IllegalStateException {
if ( isShutdown ) {
throw new ShutdownException();
}
if ( message != null ) {
throw new IllegalStateException();
}
}