Sei sulla pagina 1di 3

11/21/2016 Difference between synchronized block and method in Java | Java67

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

core java (294) Difference between synchronized block and method in Java core java interview questions


programming (185) programming interview questions
Synchronized block and synchronized methods are two ways to
core java interview question answer SQL interview questions
(86) use synchronized keyword in Java and implement mutual
data structure interview question
Java collection tutorial (71) exclusion on critical section of code. Since Java is mainly used to
coding interview questions
interview questions (56) write multi­threading programs,  which present various kinds of
java collection interview questions
coding (52) thread related issues like thread­safety, deadlock and race
java design pattern interview
java (50) conditions, which plagues into code mainly because of poor questions
Coding Problems (41) understanding of synchronization mechanism provided by Java thread interview questions
Java programming Tutorial (32) programming language. Java provides inbuilt synchronized and struts interview questions
error and exception (30) hibernate interview questions
volatile keyword to achieve synchronization in Java. Main
data structure and algorithm (23) jdbc interview questions
difference between synchronized method and synchronized block
homework (22) array interview questions
is selection of lock on which critical section is locked.
collection tutorial example (19) j2ee interview questions
Synchronized method depending upon whether its a static
Java 8 (17) spring interview questions
method or non static locks on either class level lock or object lock. Class level lock is one for each class and represented
J2EE (14) OOP Interview Questions
by class literal e.g. Stirng.class. Object level lock is provided by current object e.g. this instance, You should never mix static
String (14) UNIX Interview Questions
and non static synchronized method in Java.. On the other hand synchronized block locks on monitor evaluated by expression
books (14)
provided as parameter to synchronized block. In next section we will see an example of both synchronized method and
thread (14)
synchronized block to understand this difference better.
sql (13)
SQL interview Question (12)
Java 5 tutorial (11)
array (11)
date and time (11) Difference between synchronized method vs block in Java
java concurrency tutorial (11) Here are Some more differences between synchronized method and block in Java based upon experience and

*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/difference­between­synchronized­block­vs­method­java­example.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 multi­threading 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)

Java Programming Java SE8 for the Really The Algorithm Design ►  2015 (103)


$4.99 $6.99 $30.86 $39.99 $34.32 $39.99 $73.99 $89.95
Interviews Exposed Impatient: A Short Cour… Manual ►  2014 (67)
 (50)  (15)  (35)  (121)
▼  2013 (44)
►  December (1)
►  November (2)
►  August (7)
►  July (4)
►  June (2)
Java 8 in Action: Lambdas, Clean Code: A Handbook of Cracking the Coding Head First Java, 2nd Edition ►  May (3)
Streams, and functional­st… Agile Software Craftsma… Interview: 150 Progr…
►  April (1)
$37.71 $49.99 $39.99 $49.99 $30.00 $39.95 $29.91 $44.95
►  March (7)
 (72)  (431)  (488)  (572)
►  February (7)

All Search Amazon Go ▼  January (10)


Difference between Set, List and
Ads by Amazon
Map in Java ­ Int...
Difference between URL, URI and
URN ­ Interview Qu...
Posted by Javin Paul    +2   Recommend this on Google Difference between Self and Equi
Join in SQL ­ INN...
Labels: core java, core java interview question answer, thread
Java 7 Improved Exception
Handling ­ Multiple Catc...
5 comments: Difference between synchronized
block and method i...
Difference between Callable and
Anonymous February 17, 2013 at 8:30 PM
Runnable in Java ­...
Synchronized method is overkill, instead try to avoid synchronization from start.
10 Programming questions and
Reply exercises for Java Pr...
10 AWT Swing Interview
Questions Answers in Java
Anonymous March 4, 2013 at 5:36 AM How to use ALTER COMMAND in
A good insight. Thank You! SQL ­ database tutoria...

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/difference­between­synchronized­block­vs­method­java­example.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

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

Powered by Blogger.

StumbleUpon

Like

Share

Tweet
0

http://www.java67.com/2013/01/difference­between­synchronized­block­vs­method­java­example.html 3/3

Potrebbero piacerti anche