Facebook
From Silly Parakeet, 4 Years ago, written in Plain Text.
This paste is a reply to Re: Project from Ivory Tamarin - go back
Embed
Viewing differences between Re: Project and Re: Re: Project
package com.abc.handoff;

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 message;
         
    public StringHandoffImpl() {
//set the message to null to use it as a flag
             
message = null;
    }
//passing string calls this function
null;

    private boolean isReceiving = false;


    @Override
    public synchronized void pass(String msg, long msTimeout)
                    throws InterruptedException,
                   TimedOutException,
                   ShutdownException,
                   
InterruptedException, TimedOutException, ShutdownException, IllegalStateException {
            
//if 
{

        throwIfShutdownOrPassing();
        passAndNotify(msg);

        long messagesend = System.currentTimeMillis() + msTimeout;
        while ( 
message is != null change the message
        if (message == null) 
{
            message long messagewait msg;
            notifyAll();
        }
//wqait 
messagesend - System.currentTimeMillis();
            
if message isn't null
        if (msTimeout == 0L) {
            while (message != null) 
( messagewait <= 0 ) {
                wait();
throw new TimedOutException();
            }

            wait(messagewait);

            if ( isShutdown ) {
                throw new ShutdownException();
            }
            message = msg;\n            notifyAll();\n        }

        long endTime = System.currentTimeMillis() + msTimeout;\n        long msRemaining = msTimeout;\n//Wait for a certain time, correcting for accuracy\n        while (message != null && msRemaining > 0L) {\n            wait(msRemaining);\n            msRemaining = endTime - System.currentTimeMillis();\n        }\n        if (message == null) {\n            message = msg;\n            notifyAll();\n        } else {\n            throw new TimedOutException();\n        }\n    }\n//same as previous method with no timeout limitation\n    }


    @Override
    public synchronized void pass(String msg)
            
msg) throws InterruptedException,
                   ShutdownException,
                   
InterruptedException, ShutdownException, IllegalStateException {

               
{

        throwIfShutdownOrPassing();
        passAndNotify(msg);

        
while (message != null) {
               wait();
           }
           
message = msg;
           notifyAll();
    }
//Receiving thread's function
!= null ) {
            wait();

            if ( isShutdown ) {
                throw new ShutdownException();
            }
        }
    }


    @Override
    public synchronized String receive(long msTimeout)
                    throws InterruptedException,
                   TimedOutException,
                   ShutdownException,
                   
InterruptedException, TimedOutException, ShutdownException, IllegalStateException {
//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 endTime messagesend = System.currentTimeMillis() + msTimeout;
         long msRemaining = msTimeout;\n\n                 while (message ( message == null && msRemaining > 0L) {
             wait(msRemaining);
             msRemaining 
) {
            long messagewait 
endTime messagesend - System.currentTimeMillis();
         }\n                     if (message != null) {
             receivedMessage = message;
             message = null;
             notifyAll();
             return receivedMessage;
         }
         
( messagewait <= 0 ) {
                
throw new TimedOutException();
    }
//same method as above without the timeout limitation
TimedOutException();
            }

            wait(messagewait);

            if ( isShutdown ) {
                throw new ShutdownException();
            }
        }

        return receiveAndNotify();
    }


    @Override
    public synchronized String receive()
            
receive() throws InterruptedException,
                   ShutdownException,
                   
InterruptedException, ShutdownException, IllegalStateException {

               
{

        throwIfShutdownOrReceiving();

        // 
while (message == null) {
               wait();
           }
           String receivedMessage = message;
           
there is no message, wait.
        while ( 
message = null;
           notifyAll();
           
== null ) {
            wait();

            if ( isShutdown ) {
                throw new ShutdownException();
            }
        }

        
return receivedMessage;
    }

receiveAndNotify();
    }


    @Override
    public synchronized void shutdown() {
        throw new RuntimeException("not implemented yet"); // FIXME
isShutdown = true;
        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();
        }
    }
}}