Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does Synchronisazion prevent from readOnly-Operations?  RSS feed

 
Rudolf Schmidt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a object-variable (Vector m_vector) that is accessed by several threads because its a singleton class. I want to prevent access to the get-Method of this Vector if some Thread calls a update-Method which updates the vector. I have a synchronized(m_vector) {...} inside the update-Method. Can a Thread successfully call a singleton.getM_Vector() while another Thread executes the update-Method?
thx
Rudolf
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, unless you synchronize the get method as well.
- Peter
 
Rudolf Schmidt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply. What I don�t understand is, that although I have synchronized an object in one method (in update method with synchronized m_vector) another thread has access to that object via another method.
What does it help, if i synchronize the get-Method. Even though one thread could call the get-Method and read the vectors attributes while another threads updates via the update-Method?
thx
Rudolf
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronizing just the one method doesn't really help in general. All thread-unsafe operations on the object should be protected against multi-threaded access. Please note
  • "thread-unsafe" operations. Most operations are thread-unsafe (even simple reads), except perhaps simple operations on volatile primitive variables. For example, Vector.size() is safe(ish) even though it is not synchronized, because all it does is read the value of an instance variable.
  • "operations", not "methods". For instance, if you have a private method that will only ever be called from other methods which are themselves synchronized, the private method needs no synchronization. Similarly, a method that will only be called from within a synchronized block (e.g. java.util.Iterator implementations) doesn't need any additional synchronization either.
  • "protected against multi-threaded access", not necessarily "synchronized". Swing classes are a good example. Most of Swing is not threadsafe, i.e. the methods should only be called from one thread, the GUI thread. This restriction protects them. But a few methods are intended to be used in a multi-threaded fashion and therefore protected using synchronization.
  • HTH
    - Peter
    [ March 21, 2002: Message edited by: Peter den Haan ]
     
    Rudolf Schmidt
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    thx again. I solved my problem with a synchronized (m_vector) {} contruct in both the update and get-Method, so a Thread has to wait to get a lock on m_vector, if anotherone already has it.
    Rudolf
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!