Facebook
From Ample Hog, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 61
  1. package com.abc.handoff;
  2.  
  3. import com.abc.pp.stringhandoff.*;
  4. import com.programix.thread.*;
  5.  
  6. //This program has multiple threads passing a string to other threads
  7.  
  8. public class StringHandoffImpl implements StringHandoff {
  9. //String to hold the message to be passed
  10.         private String message;
  11.          
  12.     public StringHandoffImpl() {
  13. //set the message to null to use it as a flag
  14.          message = null;
  15.     }
  16. //passing string calls this function
  17.     @Override
  18.     public synchronized void pass(String msg, long msTimeout)
  19.             throws InterruptedException,
  20.                    TimedOutException,
  21.                    ShutdownException,
  22.                    IllegalStateException {
  23.        
  24. //if message is null change the message
  25.         if (message == null) {
  26.             message = msg;
  27.             notifyAll();
  28.         }
  29. //wqait if message isn't null
  30.         if (msTimeout == 0L) {
  31.             while (message != null) {
  32.                 wait();
  33.             }
  34.             message = msg;
  35.             notifyAll();
  36.         }
  37.  
  38.         long endTime = System.currentTimeMillis() + msTimeout;
  39.         long msRemaining = msTimeout;
  40. //Wait for a certain time, correcting for accuracy
  41.         while (message != null && msRemaining > 0L) {
  42.             wait(msRemaining);
  43.             msRemaining = endTime - System.currentTimeMillis();
  44.         }
  45.         if (message == null) {
  46.             message = msg;
  47.             notifyAll();
  48.         } else {
  49.             throw new TimedOutException();
  50.         }
  51.     }
  52. //same as previous method with no timeout limitation
  53.     @Override
  54.     public synchronized void pass(String msg)
  55.             throws InterruptedException,
  56.                    ShutdownException,
  57.                    IllegalStateException {
  58.  
  59.            while (message != null) {
  60.                wait();
  61.            }
  62.            message = msg;
  63.            notifyAll();
  64.     }
  65. //Receiving thread's function
  66.     @Override
  67.     public synchronized String receive(long msTimeout)
  68.             throws InterruptedException,
  69.                    TimedOutException,
  70.                    ShutdownException,
  71.                    IllegalStateException {
  72. //variable to hold passing message
  73.          String receivedMessage;
  74.          if (message != null) {
  75. //If message is not null recieve message change flag notify threads              
  76.              receivedMessage = message;
  77.              message = null;
  78.              notifyAll();
  79.              return receivedMessage;
  80.          }
  81.  
  82.          if (msTimeout == 0L) {
  83.              while (message == null) {
  84.                  wait();
  85.              }
  86.              receivedMessage = message;
  87.              message = null;
  88.              notifyAll();
  89.              return receivedMessage;
  90.          }
  91. //wait if message isn't available correct for time
  92.          long endTime = System.currentTimeMillis() + msTimeout;
  93.          long msRemaining = msTimeout;
  94.  
  95.          while (message == null && msRemaining > 0L) {
  96.              wait(msRemaining);
  97.              msRemaining = endTime - System.currentTimeMillis();
  98.          }
  99.          if (message != null) {
  100.              receivedMessage = message;
  101.              message = null;
  102.              notifyAll();
  103.              return receivedMessage;
  104.          }
  105.          throw new TimedOutException();
  106.     }
  107. //same method as above without the timeout limitation
  108.     @Override
  109.     public synchronized String receive()
  110.             throws InterruptedException,
  111.                    ShutdownException,
  112.                    IllegalStateException {
  113.  
  114.            while (message == null) {
  115.                wait();
  116.            }
  117.            String receivedMessage = message;
  118.            message = null;
  119.            notifyAll();
  120.            return receivedMessage;
  121.     }
  122.  
  123.     @Override
  124.     public synchronized void shutdown() {
  125.         throw new RuntimeException("not implemented yet"); // FIXME
  126.     }
  127.  
  128.     @Override
  129.     public Object getLockObject() {
  130.         return this;
  131.     }
  132. }

Replies to Project rss

Title Name Language When
Re: Project Ivory Tamarin text 3 Years ago.