• 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

NX: Thread Safety

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using the multiple instances of Data/WeakHashMap Solution.
The data structure I use to store an instance of Data's locks is synchronized, I'm wondering if this is actually need as each client gets it's own instance of Data. Therefore myLockedRecords will never be accessed by multiple threads. Any Thoughts.
Tony
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,

Therefore myLockedRecords will never be accessed by multiple threads.


Are you sure ? As all your Data instances share a common myLockedRecords structure, the latter will be accessed by multiple client connections, each of them running in its own thread.
Best,
Phil.

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note also that if you use RMI, one client may be served by more than one thread on the server. (Or different clients may be served by the same thread, at different times.) There's no way (well, no good way) to control this behavior, as the RMI spec allows different implementations to handle threading in different ways. If you're using RMI, you need to design your remote objects to be thread-safe, period.
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers,
I hoped someone would bring RMI up because I don't understand it. If my connection factory supplies a new Instance ( wrapped of course) of Data how can any other threads access the non static data structures within that instance. I maybe missing something with RMI.
Can anyne shed some light or direct me to some light.
Just re-read Jims reply, I think I understand, If understand corectly more than one thread may do work on the server side for my one thread on the client side. Therefore Thread safety is required. Am I right.
Tony
[ September 24, 2003: Message edited by: Tony Collins ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,

If my connection factory supplies a new Instance ( wrapped of course) of Data how can any other threads access the non static data structures within that instance. I maybe missing something with RMI.


Start with an example. Let's say you have two clients ("A" and "B"), and they connect to two instances of your Data class ("Y" and "Z"). The following are all possible:
  • Client "A" calls method 1 on "Y" using thread 1 which completes
  • Client "B" calls method 1 on "Z" using thread 1 which completes
  • Client "A" calls method 2 on "Y" using thread 1
  • Client "B" calls method 2 on "Z" using thread 2
  • Client "A"'s call to method 2 on "Y" completes
  • Client "A" calls method 2 on "Y" using thread 3


  • Your connection factory will ensure that client "A" only ever uses instance "Y" of Data, and client "B" will only ever use instance "Z" of Data.
    However there is no guarantee and no way of knowing which thread will be used by either client. For non concurrent access, they may both happen to use the same thread. For consecutive access one client may use two totally different threads.
    Totally confused yet
    So in your WeakHashMap you can use the instance of Data as your identifier, but you cannot use the thread as the identifier.
    Regards, Andrew
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I see but can an instance of my class be accessed by 2 threads concurrently.
    Or is that a grey area, so you should make everything thread safe anyway
    My class is thread safe but I like to justify why it is ?
    Tony
    [ September 25, 2003: Message edited by: Tony Collins ]
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Tony,
    If you have one Data instance per client, Data by itself doesn't need to be thread-safe, because any Data instance will never be accessed by more than one thread at a given time.
    But all accesses to structures shared by Data instances must be thread-safe.
    Regards,
    Phil.
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well apparentally this isn't the case when using RMI.
    Tony
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Well apparentally this isn't the case when using RMI.


    Yes it is, Tony ! Even with RMI. A given client won't call more than one Data method at a time, right ? So if every client has its own Data instance (that other clients ignore even the existence), how could a Data instance be accessed by multiple threads concurrently ?!
    As Andrew explained above, a given Data instance will be accessed by multiple threads, but successively.
    Best,
    Phil.
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I agree and that's what I'm struggling with, as I've read posts to the contary, note Jims post above.
    I also note in Max's book
    Is RMI thread safe ?
    No. The J2SE 1.4 RMI spec makes no guarantee regarding the number of threads that will hava access to your remote object. For this reason your design has to take into account issues of thread safety.
    Though I can't generate a scenario myself where this may happen.
    Tony
    [ September 25, 2003: Message edited by: Tony Collins ]
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Tony,

    Though I can't generate a scenario myself where this may happen.


    I had to really force the issue, but here is some code to prove the non thread safety issue:

    50% of the time this runs as though everything is fine. However sometimes when I ran this, I get output similar to:

    Notice the last value? Because there is no thread safety in my server code, the local value is out of sync with the global value.
    By the way: the use of the atan() method was just to do something that chews up CPU (so therefore a preemptive JVM may swap the thread out). I deliberately avoided using yield() as some people might not accept that as proof.
    Does this demonstrate what you knew but couldn't prove?
    Regards, Andrew
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I can see its happened but I'm not sure how they got out of Sync nothing else seems to update aValue other than the method called.
    Tony
     
    Ranch Hand
    Posts: 555
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Tony,
    I agree with all statements of Andrew, but I disagree with yours one:

    Which implies that all calls to a remote method need to be synchronized even if I'm Sychronizing on the data structure i'm updating within the method.


    Nope! It depends on you overall design!!!
    Two samples:
    1) If only local variables are used (no class member variables) in your remote class and you are synchrornizing on data structure, there is no need to synchronize your remote method.
    2) I have remote factory which creates s separate remote object for every client. The only shared object by all remote objects is Data class (data structure), which is synchronizined. In this case even if you have class member variables (as long as none of them exept Data is not static) you don't need to synchronize methods in your Remote Interface, since each of them is accessed by a single client thread.
    So, I for example, don't synchronize any of the methods in my Remote Class.
    Your statement is only and only true if you have multiple threads accessing your remote object and the Remote class has member variables shared accross these threads!!!
    Best,
    Vlad
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ohh I see, but my point is that if I have a 1 to 1 relationship with the Client Thread and a Remote RMI object, the instance data structures that are not static don't need to be Sychronized data structure( as in Hashtable as opposed to HashMap). Am I right here.
    Tony
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I convinced my self I didn't understand threading when I did. I think I didn't explain my initial question corectelly.
    If I have a remote object(data) that has a 1 - 1 relationship with the client that created it. My instance variable the hold locks for that instance of Data/Client does not need to be a synchronized data structure.
    Am I right here.
    Tony
    [ September 25, 2003: Message edited by: Tony Collins ]
     
    Vlad Rabkin
    Ranch Hand
    Posts: 555
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Tony,
    Is your member variable static(shared across other Remote object and therefore multiple threads)?
    Vlad
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Tony,

    If I have a remote object(data) that has a 1 - 1 relationship with the client that created it. My instance variable the hold locks for that instance of Data/Client does not need to be a synchronized data structure.


    Mmh... that's what I tried to explain above. Sorry if it was not clear !
    Best,
    Phil.
     
    Tony Collins
    Ranch Hand
    Posts: 435
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah I'm a bit slow today, But I'm making my class completely thread safe
    anway even if it's not required. The only method in my Data class that is not thread safe is the static method that caches the Database, though as this is only called once, I thought it was OK not to be completelly thread safe.
    Tony
    [ September 25, 2003: Message edited by: Tony Collins ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic