Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id$
   * 
   * Firebird Open Source J2EE Connector - JDBC Driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 package org.firebirdsql.pool;

Object stack that implements a blocking LIFO (last-in-first-out) stack. The implementation will block when the list is empty.

Author(s):
Steven Jardine
Mark Rotteveel
 
 public final class BlockingStack {

Container class for objects in the stack.
 
 	private static class Node {
 		private final Node next;
 
 		private final Object object;

Creates a Node with the specific object and next node.

Parameters:
object
next
 
 		private Node(Object objectNode next) {
 			this. = object;
 			this. = next;
 		}
 
 		private Node getNext() {
 			return ;
 		}
 
 		private Object getObject() {
 			return ;
 		}
 	}

Actual top of the stack.
 
 	private Node top = null;
 
 	private final Object topLock = new Object();

Checks to see if the stack is empty.

Returns:
true if empty, false otherwise.
 
 	public boolean isEmpty() {
 		synchronized () {
 			return ( == null);
 		}
 	}

Return, but do not remove, the object at the top of the stack.

Returns:
the object at the top of the stack, null if stack is empty.
 
 	public Object peek() {
 		synchronized () {
 			return  == null ? null : .getObject();
 		}
 	}
 
 	private Object extract() {
 		Object result = null;
 		synchronized () {
 			if ( != null) {
 				Node item = ;
 				 = item.getNext();
 				result = item.getObject();
 			}
 			if ( != null) {
 			}
		}
		return result;
	}

Return and remove the object at the top of the stack. If the stack is empty, wait until an object exists.

Returns:
the object at the top of the stack.
	public Object pop() throws InterruptedException {
		if (Thread.interrupted())
			throw new InterruptedException();
		synchronized () {
			while (isEmpty()) {
				try {
catch (InterruptedException e) {
					throw e;
				}
			}
			return extract();
		}
	}

Return and remove the object at the top of the stack only if it is available within the specified number of milliseconds, otherwise return null.

Parameters:
msec to wait for an object before returning null.
Returns:
the object at the top of the stack.
Throws:
java.lang.InterruptedException
	public Object pop(final long msecthrows InterruptedException {
		if (Thread.interrupted())
			throw new InterruptedException();
		long wait = msec;
		long start = System.currentTimeMillis();
		synchronized () {
			while (isEmpty() && wait > 0) {
				try {
					wait = msec - (System.currentTimeMillis() - start);
catch (InterruptedException e) {
					throw e;
				}
			}
			return extract();
		}
	}

Push an object onto the stack. If the stack is unavailable, wait until it becomes available.

Parameters:
item to be pushed onto the stack.
Throws:
java.lang.InterruptedException
	public void push(Object itemthrows InterruptedException {
		if (item == null)
		if (Thread.interrupted())
			throw new InterruptedException();
		synchronized () {
			 = new Node(item);
			// Notify waiting threads.
		}
	}
New to GrepCode? Check out our FAQ X