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()


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?

189 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
  30. Pingback: vitapulse
  31. Pingback: Vita pulse
  32. Pingback: vitapulse reviews
  33. Pingback: vitapulse review
  34. Pingback: pure colon detox
  35. Pingback: pure colon detox
  36. Pingback: somnapure
  37. Pingback: perfect biotics
  38. Pingback: reviews vitapulse
  39. Pingback: vitapulse vitamins
  40. Pingback: vitapulse scam
  41. Pingback: vitapulse
  42. Pingback: vitapulse
  43. Pingback: vita pulse
  44. Pingback: probiotic america
  45. Pingback: taurus traits
  46. Pingback: Diabetes Destroyer
  47. Pingback: Bodyweight Bundle
  48. Pingback: Alexapure Pro
  49. Pingback: Ecomaxx Reviews
  50. Pingback: T90 Xplode Review
  51. Pingback: Maxtropin Review
  52. Pingback: Tea Tox Reviews
  53. Pingback: Forever Bust
  54. Pingback: MegaDrox Review
  55. Pingback: Test X180 Review
  56. Pingback: vitapulse
  57. Pingback: probiotic america
  58. Pingback: Perfect Biotics
  59. Pingback: drones for sale
  60. Pingback: Homepage
  61. Pingback: Source
  62. Pingback: check over here
  63. Pingback: news
  64. Pingback: Source
  65. Pingback: a fantastic read
  66. Pingback: find this
  67. Pingback: click site
  68. Pingback: find this
  69. Pingback: Hemorrhoid No More
  70. Pingback: see it here
  71. Pingback: this contact form
  72. Pingback: Cash Blurbs
  73. Pingback: news
  74. Pingback: Clicking Here
  75. Pingback: additional hints
  76. Pingback: Visit Website
  77. Pingback: my response
  78. Pingback: directory
  79. Pingback: see this here
  80. Pingback: more info here
  81. Pingback: look at this site
  82. Pingback: check my site
  83. Pingback: more info here
  84. Pingback: her latest blog
  85. Pingback: my response
  86. Pingback: official site
  87. Pingback: additional hints
  88. Pingback: click here
  89. Pingback: tires
  90. Pingback: rims
  91. Pingback: wheels
  92. Pingback: read more
  93. Pingback: click here
  94. Pingback: read more
  95. Pingback: click here

Comments are closed.