I'm writing a series of methods to support reading, writing, swapping,
and searching for elements in a linked list in a multi-threaded
environment. The main problem is I'm not sure how to reference and use
the same linked list throughout. For example, if I'm trying to read an
element from the linked list, and the element doesn't exist, I want to
wait until it does exist. I'm using wait / notify to do this, but
after notifying that a new element exists, the wait just keeps on
waiting without breaking out of it.
Can anyone recommend general design guidelines for constructing classes
which would support this behavior? I've tried many different things,
and I still can't get the results I need.
Thanks,
xabo - 21 Feb 2006 09:13 GMT
see the producer cosumer exmaple in
http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html
this is closely related to what you want to achieve
Chris Uppal - 21 Feb 2006 09:19 GMT
> Can anyone recommend general design guidelines for constructing classes
> which would support this behavior? I've tried many different things,
> and I still can't get the results I need.
Get hold of a copy of Doug Lea's book: "Concurrent Programming in Java: Design
Principles and Patterns", and read it.
It's that simple...
Also (/not/ "instead"), if you are using J1.5 then see the new
java.util.concurrent package.
-- chris
tom fredriksen - 21 Feb 2006 23:13 GMT
> I'm writing a series of methods to support reading, writing, swapping,
> and searching for elements in a linked list in a multi-threaded
> environment.
First off, read the recommended literature in the other posts.
Basically, write an ordinary linked list, plus the following rule, only
one thread can access when changing the structure.
> The main problem is I'm not sure how to reference and use
> the same linked list throughout.
What do you mean by this, cause I dont understand how your example
explains your statement.
> For example, if I'm trying to read an
> element from the linked list, and the element doesn't exist, I want to
> wait until it does exist.
> I'm using wait / notify to do this, but
> after notifying that a new element exists, the wait just keeps on
> waiting without breaking out of it.
When you add a new object, you need to check a private list which
contains the things you are waiting for and which performs a notify if
it finds it in the list, but only after it has been inserted.
Hope that helps. If not, I wont bother you again:)
/tom