001: package com.quadcap.util.threads;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Vector;
042:
043: import com.quadcap.util.Debug;
044: import com.quadcap.util.DList;
045: import com.quadcap.util.DListItem;
046: import com.quadcap.util.ListException;
047:
048: /**
049: * This class implements a thread-safe stream data structure, where the
050: * stream has a fixed-size buffer of Objects.
051: *
052: * @author Stan Bailes
053: */
054: public class Stream {
055: int maxSize = 10;
056: DList queue = new DList();
057: boolean full = false;
058: boolean closed = false;
059:
060: /**
061: * Construct a new stream using defaults
062: */
063: public Stream() {
064: }
065:
066: /**
067: * Construct a new stream with a specified buffer size.
068: *
069: * @param maxSize the maximum number of items to buffer.
070: */
071: public Stream(int maxSize) {
072: this .maxSize = maxSize;
073: }
074:
075: /**
076: * Return the next item from the stream. Block if the stream's empty.
077: *
078: * @return the next stream item.
079: */
080: public Object read() {
081: Object obj = null;
082: synchronized (queue) {
083: while (queue.size() == 0) {
084: if (closed) {
085: return new RuntimeException("stream closed");
086: }
087: try {
088: queue.wait();
089: if (closed)
090: throw new RuntimeException("stream closed");
091: } catch (InterruptedException e) {
092: Debug.print(e);
093: }
094: }
095: try {
096: DListItem d = queue.popFront();
097: obj = d.obj;
098: } catch (ListException e) {
099: Debug.print(e);
100: }
101: if (full) {
102: full = false;
103: queue.notifyAll();
104: }
105: }
106: return obj;
107: }
108:
109: /**
110: * Write an item to the stream. Block if the stream buffer is full.
111: *
112: * @param obj the object to write to the stream.
113: */
114: public void write(Object obj) {
115: synchronized (queue) {
116: while (queue.size() >= maxSize) {
117: if (closed) {
118: throw new RuntimeException("stream closed");
119: }
120: full = true;
121: try {
122: queue.wait();
123: } catch (InterruptedException e) {
124: Debug.print(e);
125: }
126: }
127: queue.addBack(obj);
128: if (queue.size() == 1)
129: queue.notifyAll();
130: }
131: }
132:
133: /**
134: * Close this stream.
135: */
136: public void close() {
137: synchronized (queue) {
138: closed = true;
139: queue.notifyAll();
140: }
141: }
142: }
|