Differences and usage senarios of synchronised method and synchronised block ?

Questions by gopireddyshilpa

Showing Answers 1 - 3 of 3 Answers

hemasundar

  • Jan 1st, 2008
 

We use synchronized methods or synchronised blocks to make your application thread safe. 
You can use synchronized methods, but the problems may be:
1. It degrades the performance of your application.

2. Slows down the your program because synchronozation restricts concurrency.

3. Synchronized methods can lead to dead lock.


To avoid the problem(s), we can use synchronized blocks within a method. You choose areas of methods(for example,may be call to another methods) use synchronozed block, but  in this case you've to provide an argument that is the object whose key the thred needs to get.
 see the example
 
           public void doIt(){
             ........................................
             ........................................
                   synchronized(this){
                      criticalData();
                      moreCriticalData();
                    }
             ........................................
             ........................................

        }

            

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions