Facebook
From Silly Parakeet, 3 Years ago, written in Plain Text.
This paste is a reply to Re: Project from Ivory Tamarin - view diff
Embed
Download Paste or View Raw
Hits: 58
  1. package com.abc.handoff;
  2.  
  3. import com.abc.pp.stringhandoff.*;
  4. import com.programix.thread.*;
  5.  
  6.  
  7. public class StringHandoffImpl implements StringHandoff {
  8.  
  9.  
  10.     private boolean isShutdown = false;
  11.  
  12.     private String message = null;
  13.  
  14.     private boolean isReceiving = false;
  15.  
  16.  
  17.     @Override
  18.     public synchronized void pass(String msg, long msTimeout)
  19.         throws InterruptedException, TimedOutException, ShutdownException, IllegalStateException {
  20.  
  21.         throwIfShutdownOrPassing();
  22.         passAndNotify(msg);
  23.  
  24.         long messagesend = System.currentTimeMillis() + msTimeout;
  25.         while ( message != null ) {
  26.             long messagewait = messagesend - System.currentTimeMillis();
  27.             if ( messagewait <= 0 ) {
  28.                 throw new TimedOutException();
  29.             }
  30.  
  31.             wait(messagewait);
  32.  
  33.             if ( isShutdown ) {
  34.                 throw new ShutdownException();
  35.             }
  36.         }
  37.  
  38.     }
  39.  
  40.  
  41.     @Override
  42.     public synchronized void pass(String msg) throws InterruptedException, ShutdownException, IllegalStateException {
  43.  
  44.         throwIfShutdownOrPassing();
  45.         passAndNotify(msg);
  46.  
  47.         while ( message != null ) {
  48.             wait();
  49.  
  50.             if ( isShutdown ) {
  51.                 throw new ShutdownException();
  52.             }
  53.         }
  54.     }
  55.  
  56.  
  57.     @Override
  58.     public synchronized String receive(long msTimeout)
  59.         throws InterruptedException, TimedOutException, ShutdownException, IllegalStateException {
  60.  
  61.         throwIfShutdownOrReceiving();
  62.  
  63.         long messagesend = System.currentTimeMillis() + msTimeout;
  64.         while ( message == null ) {
  65.             long messagewait = messagesend - System.currentTimeMillis();
  66.             if ( messagewait <= 0 ) {
  67.                 throw new TimedOutException();
  68.             }
  69.  
  70.             wait(messagewait);
  71.  
  72.             if ( isShutdown ) {
  73.                 throw new ShutdownException();
  74.             }
  75.         }
  76.  
  77.         return receiveAndNotify();
  78.     }
  79.  
  80.  
  81.     @Override
  82.     public synchronized String receive() throws InterruptedException, ShutdownException, IllegalStateException {
  83.  
  84.         throwIfShutdownOrReceiving();
  85.  
  86.         // while there is no message, wait.
  87.         while ( message == null ) {
  88.             wait();
  89.  
  90.             if ( isShutdown ) {
  91.                 throw new ShutdownException();
  92.             }
  93.         }
  94.  
  95.         return receiveAndNotify();
  96.     }
  97.  
  98.  
  99.     @Override
  100.     public synchronized void shutdown() {
  101.         isShutdown = true;
  102.         message = null;
  103.         isReceiving = false;
  104.  
  105.         notifyAll();
  106.     }
  107.  
  108.     @Override
  109.     public Object getLockObject() {
  110.         return this;
  111.     }
  112.  
  113.  
  114.     private synchronized void passAndNotify(String msg) {
  115.         if (msg == "orange") {
  116.             System.out.println(isReceiving);
  117.         }
  118.         message = msg;
  119.         notifyAll();
  120.     }
  121.  
  122.  
  123.     private synchronized String receiveAndNotify() {
  124.         String msg = message;
  125.         message = null;
  126.         isReceiving = false;
  127.  
  128.         notifyAll();
  129.  
  130.         return msg;
  131.     }
  132.  
  133.  
  134.     private synchronized void throwIfShutdownOrReceiving() throws ShutdownException, IllegalStateException {
  135.         if ( isShutdown ) {
  136.             throw new ShutdownException();
  137.         }
  138.  
  139.         if ( isReceiving ) {
  140.             throw new IllegalStateException();
  141.         }
  142.  
  143.         isReceiving = true;
  144.     }
  145.  
  146.     private synchronized void throwIfShutdownOrPassing() throws ShutdownException, IllegalStateException {
  147.         if ( isShutdown ) {
  148.             throw new ShutdownException();
  149.         }
  150.  
  151.         if ( message != null ) {
  152.             throw new IllegalStateException();
  153.         }
  154.     }
  155. }
  156.  
  157.