Circular linked list

S

Sharp

Hi

I'm looking for a circular linked list implementation.
Any references appreciated.

Cheers
Sharp
 
S

shakah

Do you mean a ring buffer? A quick google search for "Java ring buffer"
yielded http://www.jadetower.org/muses/archives/000124.html, excerpted
here:

class BlockingMessageBuffer {
Message[] messages = new Item[BUF_SIZ];
int hd = 0, tl = 0;

public synchronized void put(Message mess) {
messages[tl] = it;
++tl;
if (tl == messages.length) tl = 0;

if (tl == hd) waitIgnoringInterrupts();
notifyAll(); // Wakes all the threads that are waiting to get.
}

public synchronized Message get() {
if (hd == tl) waitIgnoringInterrupts();
notifyAll(); // Wakes all the threads that are waiting to put.

Message mess = messages[hd];
++hd;
if (hd == messages.length) hd = 0;
return mess;
}

private void waitIgnoringInterrupts() {
while (true) {
try {
wait(); // Puts the current thread to sleep and
// frees synchronization claims on this buffer.
return;
} catch (InterruptedException e) {}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top