Hello all, I have a thread and shared resource synchronization problem. Here's the skinny on what I want my program to do. I have a class that extends Thread, call it class X. The class X object takes a String array and an integer representing the position in the array to update as the input parameters of its constructor. (This String array will be the shared resource between all instances of class X) I also have a Servlet that creates four instances of this X class and passes each instance of X the same array reference but different integer values for the array position value ranging from (0 to 3). I want to ensure that each class X object updates only one position of the array and that all X objects update the positions, as represented by the position value passed to their constructors, in the array before I attempt to access the array in order to display its contents to a user. (Since there are 4 X objects the shared String array is of size = 4) I have tried a few different approaches with no luck. I can't seem to get each object to update the array. I may get two to do an update at best. Does anyone have a design approach that would guarantee that each array element is updated by the appropriate thread? Thanks in advance, Chad Schmidt
Ok, this may help some of you help me better. Here is a program where I am trying to synchronize the update a shared String array between four Threads. This example does not include the Servlet but that's not where my problem is so I figured that I would exlude it.
Please help if you can, it is important that I figure this problem out soon. I'm not quite sure I fully understand how wait() and notify/notifyAll() work. Thank you, Chad Schmidt [ February 20, 2004: Message edited by: Jim Yingst ]
It seems to me that you have wrong understanding about synchronization between threads. My strong suggestion for you would be - get at least basic understanding when synchronization is necessary and how it works. A very good example with explanations can be found in Sun's Java Tutorial http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html After you work on this one get back to your task and you might see the solution.
Raimondas, You are 100% correct. I did not have a full understanding of how synchronization works between threads and shared objects. I did a bit of reading this weekend and I now fully understand what I was doing wrong. I had looked at the Sun tutorial you suggested but the solution did not pop out at me on Friday. I just needed to do a little more in depth reading before I saw the solution. I should be good to go now. Thanks much, Chad Schmidt
Chad Schmidt<br />ctschmidt
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop