Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Java67
Java Programming tutorials and Interview Questions Search
Home core java coding thread sql java 8 books array string j2ee oop debugging collections data structure
Categories Interview Questions
*T&C apply.
JSP (10) syntactical rules of synchronized keyword in Java. Though both block and method can be used to provide highest
Java Multithreading Tutorial (10) degree of synchronization in Java, use of synchronized block over method is considered as better Java coding
Servlet (10) practices.
java io tutorial (10)
Books and Resources
thread interview questions (10)
database (8) 6 Website to Learn JavaScript
Online
object oriented programming (8)
Top 10 Java 8 Tutorials
JDBC (6)
5 Free JavaScript Books for Web
Eclipse (5) Developers
basics (5) Best Book to Learn Java
troubleshooting (5) Programming
JEE Interview Questions (4) Top 3 Free Struts Books for Java EE
developers
*T&C apply.
JavaScript (4)
5 Books to Improve Your Coding
MySQL tutorial example (4)
Skill
design pattern (4)
StumbleUpon 10 Books Every Programmer Should
java design pattern (4) Read
Like JSP Interview Question (3) 10 Computer Algorithm Books for
Java Enum (3) Programmers
Linux (3) 10 Free Java Programming Books
1) One significant difference between synchronized method and block is that, Synchronized block generally reduce scope of lock. 5 Books to Learn Java 8 Better
OCAJP (3)
Share As scope of lock is inversely proportional to performance, its always better to lock only critical section of code. One of the best
OOPS (3)
example of using synchronized block is double checked locking in Singleton pattern where instead of locking whole
Struts (3)
Tweet getInstance() method we only lock critical section of code which is used to create Singleton instance. This improves
Web Service (3)
0
performance drastically because locking is only required one or two times.
debugging (3)
enum (3)
Hibernate interview Question (2) 2) Synchronized block provide granular control over lock, as you can use arbitrary any lock to provide mutual exclusion to critical
Related Articles section code. On the other hand synchronized method always lock either on current object represented by this keyword or class
level lock, if its static synchronized method.
3) Synchronized block can throw throw java.lang.NullPointerException if expression provided to block as parameter evaluates to
null, which is not the case with synchronized methods.
Secure your IT
career 4) In case of synchronized method, lock is acquired by thread when it enter method and released when it leaves method, either
Ad niitdiginxt.com normally or by throwing Exception. On the other hand in case of synchronized block, thread acquires lock when they enter
Submit Resume synchronized block and release when they leave synchronized block.
Now
Ad monsterindia.com Synchronized method vs synchronized block Example in Java
Difference between Here is an example of sample class which shows on which object synchronized method and block are locked and how to use
CountDownLatch...
them :
java67.com
Free Wallpaper For /**
PC's
* Java class to demonstrate use of synchronization method and block in Java
Ad Wallpapers.fm
*/
Producer
public class SycnronizationExample{
Consumer...
java67.com
Top 10 Tricky Java
interview... public synchronized void lockedByThis(){
java67.com
System.out.println(" This synchronized method is locked by current" instance of object
i.e. this");
10 Frequently
asked SQL Query... }
http://www.java67.com/2013/01/differencebetweensynchronizedblockvsmethodjavaexample.html 1/3
11/21/2016 Difference between synchronized block and method in Java | Java67
java67.com
Top 30 OOPS public static synchronized void lockedByClassLock(){
Concept Interview... System.out.println("This static synchronized method is locked by class level lock of this
java67.com class i.e. SychronizationExample.class"
);
Difference between
transient vs...
}
java67.com
Search This Blog
10 points about public void lockedBySynchronizedBlock(){
wait(), notify() and... Search
System.err.println("This line is executed without locking"
);
java67.com Recommended Reading
How to Sort The Best Book to Learn Java in 30
HashMap in Java... Object obj = String.class; //class level lock of Stirng class
days
java67.com 10 Java Web Service Interview
synchronized(obj){ Questions
Best Book to Learn
Java... System.out.println("synchronized block, locked by lock represented using obj Top 10 Android Interview Questions
for Java Programmers
java67.com variable");
How to use an ArrayList in Java?
Why Abstract class }
10 Books Every Programmer Should
is Important in... } Read
java67.com 5 Great Books to Learn Java 8
} Ads by Google
Pattern Lock
That's all on difference between synchronized method and block in Java. Favoring synchronized block over method is one of
Java Thread Synchronized
the Java best practices to follow as it reduces scope of lock and improves performance. On the other hand using synchronized Follow by Email
method are rather easy but it also creates bugs when you mix non static and static synchronized methods, as both of them are
Email address... Submit
locked on different monitors and if you use them to synchronize access of shared resource, it will most likely break.
Followers
StumbleUpon
Other Java multithreading tutorials you may like Followers (973) Next
Like
Difference between Runnable and Thread in Java
Difference between CyclicBarrier and CountDownLatch in Java
Top 5 Java Thread Interview questions with Answers
Share Difference between Thread and Process in Java
How to solve Producer consumer problem using wait and notify in Java
Tweet
0
Related Books for Further Reading Follow
Ads by Google
Java Public Static Class
Exception Class in Java
Blog Archive
8:30 ► 2016 (144)
Reply How to format Date in Java
SimpleDateFormat Exam...
► 2012 (122)
Molluru August 28, 2013 at 10:53 AM
One more advantage is using synchronized block: You can acquire lock on any object which you specify
as synchronized(Object) where as in synchronized method it looks like synchronized(this).
Reply
Replies
http://www.java67.com/2013/01/differencebetweensynchronizedblockvsmethodjavaexample.html 2/3
11/21/2016 Difference between synchronized block and method in Java | Java67
Chan December 16, 2014 at 11:42 PM
That's really a significant differnece and can dictat the use of synchornized block.
Reply
StumbleUpon
suresh chaudhari May 13, 2016 at 12:16 AM
Like Kya likha hain maja aa gaya miya
Reply
Share
Enter your comment...
Tweet
0
Comment as: Select profile...
Publish
Preview
Subscribe to: Post Comments (Atom)
Powered by Blogger.
StumbleUpon
Like
Share
Tweet
0
http://www.java67.com/2013/01/differencebetweensynchronizedblockvsmethodjavaexample.html 3/3