Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By eRaaaa
  • 1 Post By doWhile

Thread: Thread synchronization and the synchronized modifier

  1. #1
    A GJP is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Thread synchronization and the synchronized modifier

    Hello,

    I am currently developing a program that makes use of three Vectors to store connection information. These Vectors are accessed by multiple Threads that modify their data and most importantly, use methods that remove information from one Vector, converts it and then puts it into a different one.

    I do know that Vectors are synchronized, but since the methods contain multiple (synchronized) modifications in a row, I decided to synchronize these methods too in order to prevent everything getting mixed up.

    What I'm wondering about is:

    A) Is it really necessary to synchronize the entire method?
    B) Does the synchronized method modifier do anything else except creating a synchronized block with a monitor on this around the content?
    C) What is the point of allowing synchronized static methods then?

    God, lots of synchronizeds in this post O_o

    Edit: Urgh, sorry. I just noticed that there is a section for synchronization. Would be nice if someone could move it there.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Thread synchronization and the synchronized modifier

    a) maybe? if you want or if it`s required that the method is executed sequentially one after the other (atomic), then yes.
    b) not really
    c) you will lock the class and not an object!

    Intrinsic Locks and Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)
    A GJP likes this.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Thread synchronization and the synchronized modifier

    in the scjp book (p. 744) you can read

    the moral here is that just because a class is described as "thread-safe" doesn't mean it is always thread-safe. The individual methods are synchronized, that may not be enough - you may be better off putting in synchronization at a higher level (i.e., put it in the block or method that calls the other methods).

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Thread synchronization and the synchronized modifier

    A) Not necessarily. You could also use synchronized blocks:

    Java Code:
    synchronized(myVector) {
        // several modifications
    }
    If you do this, you could use a Collections class instead of a Vector, which is recommended. Vectors are deprecated.

    C) synchronized static methods synchronize on the Class object representing the class itself, rather than on an instance object.
    Get in the habit of using standard Java naming conventions!

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Thread synchronization and the synchronized modifier

    Quote Originally Posted by kjkrum View Post
    Vectors are deprecated.
    Just a clarification: Vector is not currently deprecated. But to quote the API from java 7:
    If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.
    The class does contain legacy code, as it was created prior to the Collections framework and adapter later to conform to it.
    List Implementations (The Java™ Tutorials > Collections > Implementations)
    kjkrum likes this.

Similar Threads

  1. Thread and Synchronization
    By yolo in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 03:21 AM
  2. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2011, 12:30 PM
  3. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-07-2011, 07:38 AM
  4. Thread safe without using synchronization
    By swetu.vc in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-20-2010, 08:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •