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 Does Synchronisazion prevent from readOnly-Operations? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Does Synchronisazion prevent from readOnly-Operations?" Watch "Does Synchronisazion prevent from readOnly-Operations?" New topic
Author

Does Synchronisazion prevent from readOnly-Operations?

Rudolf Schmidt
Greenhorn

Joined: Sep 05, 2001
Posts: 10
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

Joined: Apr 20, 2000
Posts: 3252
Yes, unless you synchronize the get method as well.
- Peter
Rudolf Schmidt
Greenhorn

Joined: Sep 05, 2001
Posts: 10
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

Joined: Apr 20, 2000
Posts: 3252
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

    Joined: Sep 05, 2001
    Posts: 10
    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
     
    IntelliJ Java IDE
     
    subject: Does Synchronisazion prevent from readOnly-Operations?
     
    Threads others viewed
    Thread Safe
    Applet - Servlet Communication
    JTable(!!!)
    reflection/introspection
    RelationShip Between Iterator And Vector...
    MyEclipse, The Clear Choice