Nattakan Lukkanapinit wrote:I get the concept [FILO] of stack but a little confused about Queue [ FIFO].
Anyone have a good example that use add(), remove() [ not collections or generics].
Note that add and remove are Collection methods, not Queue methods.
You could read the Javadoc for BlockingQueue (it's a special kind of queue, but the code demonstrates the principle). Queues are often used to pass work from any number of clients to one or more workers. Clients put tasks/data to process on the queue, workers, running in another thread, take and process them.
If you check the Javadocs of the Queue interface, you'll see that most implementations are concurrency-related (they have 'Blocking' or 'Concurrent' in their names), plus there are some special queues (Priority(Blocking)Queue, DelayQueue).
I don't think I've ever used a queue in a single-threaded program, but you could to it.
Joined: Apr 24, 2010
This is stack and i would like to modify it to queue
I tried to put head and tail to it.
Do i have to change when add data in or pull data out ?
This is my new add for queue
Is it ok ?
I can't find the way to pull data out by tail to head
Joined: Apr 24, 2010
I already finished add and remove but now i stuck at "print all the queue".
The problem is in queue class.
this is my print queue.
It print only the head of the queue, i dont know why.
this is my code
Joined: May 06, 2010
In the loop, head is always the same value, you never set it to the next element.
Istvan Kovacs wrote:I don't think I've ever used a queue in a single-threaded program, but you could to it.
We (sort of) do. We have dozens of applications that create jobs (documents to process). We have a single threaded job that does the processing. All the job creators write to a queue (we use MQSeries), dumping in everything as fast as they can.
The single-threaded application then reads them off one at a time to process them.
It's not EXACTLY what you meant, since we have many, many single threaded apps doing this, but the need is there.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors