Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: When to Synchronize Code

  1. #1
    Banned
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post When to Synchronize Code

    The most difficult aspect of the synchronized keyword is deciding when to use it. You pay a performance penalty for using synchronized methods. The Java 2 platform brings some new internal algorithms to the JVM to improve performance of multithreaded programs.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,609
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: When to Synchronize Code

    I moved this to the Threads forum.

    Do you have a question?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3

    Default Re: When to Synchronize Code

    He doesn't have any question, he just wanted to point out a statement regarding synchronized keyword. In fact, performance is not considered when using synchronized keyword, since the difference is very little compared to non-synchronized. The important of using synchronized code is to prevent state of an object from being modified by multiple threads at a same time which may cause unpredictable result.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,609
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: When to Synchronize Code

    No, this was spam, copied and pasted from somewhere without any actual meaning- notice that he's talking about Java 2, which is ancient. He has since been banned.

    Also, the performance penalty for synchronization is not necessarily non-trivial. This is one of the reasons people usually prefer ArrayList over Vector- Vector is synchronized, which introduces an unnecessary performance penalty in cases where synchronization is not needed, which is usually the case.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: When to Synchronize Code

    Hi Kevin,

    Sorry I didn't notice that he is 'banned'. Synchronization is not performance penalty anymore, see this.

    Also, thread-safe should be considered over performance. It's not good to sacrifice safety for a little performance.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,609
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: When to Synchronize Code

    Quote Originally Posted by Bob_Sadarka View Post
    Hi Kevin,Synchronization is not performance penalty anymore, see this.

    Also, thread-safe should be considered over performance. It's not good to sacrifice safety for a little performance.
    As that article points out, synchronization introduces a constant amount of overhead. If you don't need it, why introduce the overhead? Also, maintainability should be considered over unnecessary "thread safety" which is a non-issue in many programs that don't use multi-threading.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: When to Synchronize Code

    Adding synchronization does slow down your code. Every time the program encounters a synchronized section the program must acquire a lock for the object being synchronized. This is an expensive operation for short blocks which are run many times, but if it is necessary then you need it and had better add a synchronized section, or figure out a better way to make your code work as desired. I have seen sections of code where removing the unnecessary synchronized blocks has dramatically increased performance an order of magnitude, and other times where an unnecessary synchronized block has a negligible effect on performance.

    Also, adding synchronization everywhere you can does not always increase thread safety, or produce the desired results.

    ex.:

        private SomeType a, b;
     
        public void doIt1()
        {
            synchronized(a)
            {
                synchronized(b)
                {
                    // do something with a and b
                }
            }
        }
     
        public void doIt2()
        {
            synchronized(b)
            {
                synchronized(a)
                {
                    // do something with a and b
                }
            }
        }
    }

    This type of code can produce deadlocks if doIt1 and doIt2 are called simultaneously. This is a somewhat contrived example, but there are more elaborate pieces of code which can run into this issue when using synchronized naively. This is a block-level example, but method-level synchronized blocks are not necessarily immune to deadlocks.

    The take-away:
    If you don't need the synchronized section, don't use it (applies to any other locking mechanism, too). It's always possible for someone to add a specially tailored external locking mechanism to their application, but it's difficult/impossible for them to modify or remove internal locking.
    Last edited by helloworld922; March 2nd, 2012 at 11:45 AM.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    KevinWorkman (March 2nd, 2012)

  9. #8
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: When to Synchronize Code

    So we should use synchronization where it requires. That's what I'd like to point out.

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: When to Synchronize Code

    True, you should use synchronize when required. However, don't blindly believe that simply adding synchronized methods/blocks will solve all of your multi-threading problems, or is even a good solution if it works. One of the key goals of multi-threading is to offer a performance gain over single thread applications, and if you're unable to deliver that you should consider if multi-threading is the way to go for your particular application.
    Last edited by helloworld922; March 9th, 2012 at 11:49 AM.

  11. #10
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: When to Synchronize Code

    Yes, man. Especially in desktop apps which have GUI and background tasks running together.

  12. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: When to Synchronize Code

    Personally I consider the synchronized keyword a smell and in any projects I manage nowadays I ask the developers to consult another developer for advice if they think that synchronized is needed. Usually in most cases you do not need to use synchronized but when you do it can be very tricky to get it right.

    Having threading issues once you've gone live with a product is a proper pain, very difficult to track down.

    If you really do think you need to use synchronized try and not use it on the actual method itself but rather around the code or particular method call that needs it. Also consider using the Lock interface, it might be more appropriate.

    My conclusion of the past years have been that if you think you need to use synchronized you've probably made a design error along the way somewhere.

  13. #12
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: When to Synchronize Code

    Quote Originally Posted by Json View Post

    My conclusion of the past years have been that if you think you need to use synchronized you've probably made a design error along the way somewhere.
    Oops! the synchronized keyword is useless? who invented it for what?

  14. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,609
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: When to Synchronize Code

    Quote Originally Posted by Bob_Sadarka View Post
    Oops! the synchronized keyword is useless? who invented it for what?
    Nobody said it was useless, just misused. Nine times out of ten, if somebody is using it, they've likely backed themselves into a corner with their design or are simply misunderstanding how things are supposed to work. I would also put active rendering in this category.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2011, 12:03 AM