Problems with ArrayList using as a queue

Hi

I have a object of ArrayList and i m using as a queue.

while queueing an element from it i use toString() to use its value becouse the values that are being enqueued are of type string

before dequeuing i check it using isEmpty() funtion but some time this funtion returns false and controll goes to else case. and when it dequeue the value it throw and exception NoSuchElementException. in this case the size of arraylist is also return 0 in some case and size in some cases when there are values in the queue when isEmpty is false in case of exception

Any one can tell me the reason

it happens with irragular intervals of time.... becose values are equeqing and dequeing continously after some time interval.

Message was edited by:

12345789

[795 byte] By [12345789a] at [2007-11-26 20:52:47]
# 1
Do you use multi-threading? And how do you "dequeue"? Code please.
CeciNEstPasUnProgrammeura at 2007-7-10 2:18:29 > top of Java-index,Java Essentials,Java Programming...
# 2

this queue is populated in a servlet that recieves requests from clients

suppose 200 clients are sending requests to server and all are enqueued

with interval of 5 senconds a timer task dequeue it and process it.

// queue is of type ArrayList

for(int i=0;i<queue,size; i++){

if (queue.isEmpty()) {

break;

}

else {

String strResponse = queue.removeLast().toString();

// strResonse is used further ....

}

}>

12345789a at 2007-7-10 2:18:29 > top of Java-index,Java Essentials,Java Programming...
# 3
Any particular reason you're not using an implementation of Java's java.util.Queue interface? http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html
prometheuzza at 2007-7-10 2:18:29 > top of Java-index,Java Essentials,Java Programming...
# 4
oh sorry its Linkedlist not ArrayList
12345789a at 2007-7-10 2:18:30 > top of Java-index,Java Essentials,Java Programming...
# 5
> oh sorry its Linkedlist not ArrayListNo matter. My question still stands.
prometheuzza at 2007-7-10 2:18:30 > top of Java-index,Java Essentials,Java Programming...
# 6
OOOK but if i do using it then what is the problem or do you think if i use Queue instead of LinkedList will it remove this problem?
12345789a at 2007-7-10 2:18:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> OOOK but if i do using it then what is the problem or

> do you think if i use Queue instead of LinkedList

> will it remove this problem?

I don't know, of course. But I do know that using a built-in class, instead of a roll-your-own-queue-class, is far better. It is likely that your own queue implementation has a bug in it.

prometheuzza at 2007-7-10 2:18:30 > top of Java-index,Java Essentials,Java Programming...
# 8
and one thing more i m using jdk1.4.2 and i dont think so that there is Queue Interface
12345789a at 2007-7-10 2:18:30 > top of Java-index,Java Essentials,Java Programming...
# 9
> and one thing more i m using jdk1.4.2 and i dont> think so that there is Queue InterfaceThat is correct.
prometheuzza at 2007-7-10 2:18:30 > top of Java-index,Java Essentials,Java Programming...