This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
your prntodd method is not synchrnized. there are two threads which are running/executing code of single th object. when the first thread thread -0 enters printeven , it acquires lock on the th object thus restraining thread 1 to enter the prntevn method. when first thread is done with the prntevn method, it releases the lock which is acquired by the second thread which starts executing the prnteven code. the first thread is free to enter the prntodd method because it is not synchronized. remember when a thread acquires a lock on the object NO OTHER THREAD CAN ENTER ITS i.e. OBJECT'S synchronized parts(method/block). however non-synchronized code is free for concurrent access. also keep in mind that sleep does not release the object.
vivek dhiman wrote:Thanks for reply, it means thread - 1 also acquired the lock on prnteven function but as thread -0 has no lock on prntodd() thus the output seems mix of both of these functionality.[/quote
yes you are right , but try to be accurate in terminology. it is not lock on prnteven function. lock is aquired on object. so you should say thread -1 acquired lock on th object.