File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Shared resource synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Shared resource synchronization" Watch "Shared resource synchronization" New topic
Author

Shared resource synchronization

Chad Schmidt
Greenhorn

Joined: Nov 04, 2003
Posts: 16
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
Chad Schmidt
Greenhorn

Joined: Nov 04, 2003
Posts: 16
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 ]
raimondas zemaitis
Ranch Hand

Joined: Feb 23, 2001
Posts: 104
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.
Chad Schmidt
Greenhorn

Joined: Nov 04, 2003
Posts: 16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shared resource synchronization
 
Similar Threads
Sun Cirtification
Question about filling an Object[][]
volatile and static keywords
my notes on JLS for any1 who needs them !!
Synchronization block