In Chapter 20 of the Java Advanced Topics course (the video
on Synchronization and Thread Safety), at about 20 mins 15 seconds, I explain
that you tend to use the synchronized keyword with the smallest possible code
block. I demonstrate this with the following code:
Unfortunately, for the example we have been looking at, this
wouldn’t be thread safe! It would be possible, if a thread was interrupted
after the synchronized block, but before the return statement, for 2 threads to
be given the same number.
Thank you to one of our customers who pointed this out today
– the synchronized keyword needs to be around the minimum part of the code that
should not be interrupted. In this example it would need to be the entire
method.
Hi Matt,
ReplyDeleteI am looking for Java Trainer. If possible can share the contact here
In this blog, errata serves as a valuable clarification, emphasizing the significance of correctly applying thread synchronization in Java. It also showcases the author's commitment to ensuring the accuracy and reliability of the course materials, instilling trust and confidence in the readers.
ReplyDeleteTo learn about java, Java Certification Course in Lucknow is the right place for learning.
Thank you for sharing great content. For your digital and skill growth, please check, Digital Marketing Course in Noida
ReplyDeleteThank you for sharing this valuable content. I always appreciate well-crafted articles with excellent ideas that make the post truly enjoyable. Keep up the fantastic work, and I look forward to more from you.
ReplyDeletevisit: Ethical Considerations in Data Analytics
The article posted was very informative and useful. You people are doing a great job. Keep going.
ReplyDeletedevops-training-in-hyderabad