Tuesday, 4 July 2017

Java Advanced Tools - Errata for Thread Safety

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. 


  1. Informative post about hadoop, i am looking forward for realtime hadoop online training institute.

    1. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

  3. I have read your blog its very attractive and impressive. I like your blog. core Java online training Bangalore

  4. I really enjoy the blog.Much thanks again. Really Great core Java online course

  5. Hi Matt,

    I am looking for Java Trainer. If possible can share the contact here