Java Interview Question: Synchronized

I’ve written already about Java Interview Questions here, here and here. Another question I sometimes ask is about Javas synchronized keyword.

Can you tell me in which different ways one can use synchronized in Java and which are the pros and cons?

There are two ways to use synchronized, as a method modifier and as a code block:


public synchronized void doSomething()

and


synchronized(semaphore) {
...
}


At first I let the candidate explain the differences and how and when to use each way. Then I ask about advantages and disadvantages of each method.

Synchronized methods

Many people think there are no advantages for the method modifier, but here they are:

  • Synchronization startegy is explicit and documented in the API
  • Method is coarse, so there are less chances for deadlocks – though you can still easily create one

The disadvantages of method synchronization:

  • Coarse locking is not performant
  • Can easily lead to too much synchronization, usually developers have no clear overall locking strategy when using synchronized which leads to too many locks

Synchronized blocks

The advantages of using a synchronized block:

  • Fine tuning on what to lock on
  • Smaller code blocks than methods for better performance

The disadvantages are:

  • Can easily hold more than one lock (nested synchronized blocks), so deadlocks are easier
  • Synchronization strategy not documented, blocks are not composable so you need to look into method

You can find another discussion on methods vs. blocks on StackOverflow.

Further concurrency discussion

I then usually go into a deeper discussion on what the synchronized block can sync. You can use classes, object instances, object attributes or “this”. Each of these usages has pros and cons, mainly about the scope of the synchronized block and the strategy between concurrent threads.

If the candidate steers through all of this, I go deeper into concurrency and ask about better methods in Java for concurrent programming – what he knows and what he has used and why. This includes the data structures in java.util.concurrent, message passing and actors, STM or fork/join with work stealing.

Hope you know your concurrency and what do you think are the pros and cons of different synchronization constructs in Java?

58 thoughts on “Java Interview Question: Synchronized”

  1. Pingback: best
  2. Pingback: fucoxanthin
  3. Pingback: geniux pills
  4. Pingback: intelligex
  5. Pingback: intellux
  6. Pingback: probrain
  7. Pingback: radian c
  8. Pingback: red rhino
  9. Pingback: supreme antler
  10. Pingback: genius reviews
  11. Pingback: testomax
  12. Pingback: TRACKR BRAVO
  13. Pingback: alpha f1
  14. Pingback: alpha fuel
  15. Pingback: alpha man pro
  16. Pingback: alpha peak
  17. Pingback: alpha xtrm
  18. Pingback: anabolic rx24
  19. Pingback: cognifocus
  20. Pingback: garcinia melt
  21. Pingback: max synapse
  22. Pingback: nerventrax
  23. Pingback: nutra forskolin
  24. Pingback: testorip
  25. Pingback: zynev
  26. Pingback: pro forskolin
  27. Pingback: forskolin slim
  28. Pingback: garcinia cleanse
  29. Pingback: forskolin fuel

Comments are closed.