[concurrency-interest] LinkedQueue extensions

Michael Mattox michael.mattox@verideon.com
Wed, 18 Jun 2003 09:45:03 +0200


I need some extra functionality on the LinkedQueue method.  I extended the
class and I'd like to know if my new methods are OK.  I have very little
experience coding multithreaded code so I'd like to double check my code.
The methods I need are: contains, size, and getContents.  My app has been
hanging near the spot where I call the contains method and I wonder if there
could be a deadlock or something.

If these changes are OK, should I contribute them as a patch?

Thanks,
Michael

--

package com.verideon.veriguard.util;

import java.util.*;

import EDU.oswego.cs.dl.util.concurrent.*;

public class LinkedQueueWithContains extends
EDU.oswego.cs.dl.util.concurrent.LinkedQueue {

	public LinkedQueueWithContains() {
		super();
	}

	public boolean contains(Object o) {
		synchronized (head_) {
			if (head_ != null) {
				LinkedNode current = head_.next;
				while (current != null) {
					if (current.value.equals(o)) {
						return true;
					} else {
						current = current.next;
					}
				}
			}
			return false;
		}
	}

	public List getContents() {
		List contents = new LinkedList();

		synchronized (head_) {
			if (head_ != null) {
				LinkedNode current = head_.next;
				while (current != null) {
					contents.add(current.value);
					current = current.next;
				}
			}
		}
		return contents;
	}

	public int getSize() {
		int size = 0;
		synchronized (head_) {
			if (head_ != null) {
				LinkedNode current = head_.next;
				while (current != null) {
					size++;
					current = current.next;
				}
			}
		}
		return size;
	}
}