QJCC homepage

biz.chitec.quarterback.util
Class ThreadInterface

java.lang.Object
  extended bybiz.chitec.quarterback.util.ThreadInterface
Direct Known Subclasses:
LimitedThreadInterface, ThreadKicker

public class ThreadInterface
extends java.lang.Object

simple Interface following the multithreaded producer-consumer paradigm. Producer sends objects to consumer. Objects are stored if producer is faster than consumer.

Version:
$Id: e782cce04fd5785678463ec77e6a33b3265a487c $
Author:
Dirk Hillbrecht 1997-1999, chitec/Dirk Hillbrecht 2000 Distributed under the terms of the GNU LGPL.

Nested Class Summary
(package private)  class ThreadInterface.ChainElem
          simple subclass: element of a simple linked list.
 
Field Summary
(package private)  boolean closing
           
(package private)  boolean consumersleeping
           
(package private)  ThreadInterface.ChainElem first
           
(package private)  ThreadInterface.ChainElem worked
           
 
Constructor Summary
ThreadInterface()
          simple constructor: internal chain is initialized.
 
Method Summary
 void close()
          closes interface.
 java.lang.Object consume()
          Called by consumer: delivers next (produced) object.
 java.lang.Object consume(long waittime)
          Called by consumer: delivers next (produced) object but waits no longer than waittime milliseconds.
 boolean consumerSleeping()
          returns true if consumer is sleeping.
 boolean consumerWouldIdle()
          returns true if no input for consumer is there.
 boolean isClosing()
          Returns whether the ThreadInterface is just about closing.
 void produce(java.lang.Object o)
          called by producer: puts a new object into the internal chain and notify()s the consumer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

first

ThreadInterface.ChainElem first

worked

ThreadInterface.ChainElem worked

consumersleeping

boolean consumersleeping

closing

boolean closing
Constructor Detail

ThreadInterface

public ThreadInterface()
simple constructor: internal chain is initialized.

Method Detail

close

public void close()
closes interface. After calling this, nothing can be produced or consumed through this interface any more. A wait()ing consumer is notify()ed and call to consume() or produce() leads to a IOException. Produced but not yet consumed objects are delivered before, anyhow.


isClosing

public boolean isClosing()
Returns whether the ThreadInterface is just about closing. This method is only for reporting. It must not be used for any inherent logic.

Returns:
True iff close() has been called in advance

consume

public java.lang.Object consume()
                         throws java.io.IOException
Called by consumer: delivers next (produced) object. Waits infinitely for the next object if nothing has been produced so far.

Returns:
The next produced object.
Throws:
java.io.IOException - If the ThreadInterface is closed in the meantime.

consume

public java.lang.Object consume(long waittime)
                         throws java.io.IOException
Called by consumer: delivers next (produced) object but waits no longer than waittime milliseconds. Returns the next produced object. If something has already been produced, that one is returned immediately. Otherwise, consume() wait()s until the next object arrives. If waittime is greater than 0, consume() will not wait longer than that amount of time. If it's over and nothing has been produced, null will be returned. Otherwise, the next produced object is returned as soon as it has been produced.

If waittime is null, consume() will never return null (as long as it has not been produced). Instead, it will wait infinitely. If consume() is called on a closed ThreadInterface and nothing is left in the queue, an IOException is thrown. This is also the case if the ThreadInterface is closed while consume() is wait()ing and nothing has been added to the queue in advance.

Parameters:
waittime - Milliseconds to wait for new stuff longest. Zero for "infinite".
Returns:
Next produced element, null if time is over and nothing has been produced.
Throws:
java.io.IOException - If ThreadInterface is closed while waiting.

consumerSleeping

public boolean consumerSleeping()
returns true if consumer is sleeping. Therefore it is guaranteed that nothing is done on consumer side for the moment.


produce

public void produce(java.lang.Object o)
             throws java.io.IOException
called by producer: puts a new object into the internal chain and notify()s the consumer. This method always returns almost immideately.

Throws:
java.io.IOException

consumerWouldIdle

public boolean consumerWouldIdle()
returns true if no input for consumer is there. This method can be called from the consumer if he wants to know whether there is new input waiting. If this method is returning false, a call to consume() will return immideately. On the other hand, if it returns true, there is no garanty for the consumer to be blocked, as there could arrive new input between consumerWouldIdle() and consume().


QJCC homepage