• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shared resource synchronization

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic