What is the difference between synchronized block(statement) and synchro nized methods?

Answer:
i. syn method - all statements in this are synchronized
syn block - only this block of code is synchronized, not whole method.


ii. syn method - if already any thread is holding lock on this function's object then other threads can't call another syn method on same object.
syn block - If a function is having 1000 lines and I access shared data only in 5,6th line, then better to put syn block than making whole function as
synchronized.

No comments:

Post a Comment