[concurrency-interest] LinkedQueue extensions

Joseph Bowbeer jozart@csi.com
Wed, 18 Jun 2003 03:36:20 -0700


I'm not familiar with the Queue code in dl.util.concurrent, but I want to
point out that the proposed java.util.concurrent.LinkedQueue is a Collection
and therefore does provide contains, size, and toArray methods.


----- Original Message ----- 
From: "Michael Mattox" <michael.mattox@verideon.com>
To: <concurrency-interest@altair.cs.oswego.edu>
Sent: Wednesday, June 18, 2003 12:45 AM
Subject: [concurrency-interest] LinkedQueue extensions



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;
}
}