[Solved] Unable to understand the execution flow of the multithreading code


When it comes to “current date or time”, it means “randomness”. The reason of the different output of your code is not multi threads, but the

new java.util.Date() 

Thread switching can cause randomness also, but it is very easy to avoid because of the existence of locks. Your code also has lock by using JAVA keyword “synchronized” on methods. When a method has a “synchronized”, that equals you are using

synchronized(this) {...}

Which means different threads can not run into the same Producer object’s {…} at the same time. The producer calls notify() after putting one ele to vector, which means producer says “The vector is not empty, please fetch something from it” to consumer. The consumer calls notify() after getting one ele from vector, which means consumer says “The vector is not full, please put your ele into it” to producer.

To know these things clearly, please learning the basics of locks and conditions in JAVA.

solved Unable to understand the execution flow of the multithreading code