• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized block and methods

 
velmurugan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the difference between
Synchronized block and Synchronized methods.
Where we use Synchronized blocks and where we use Synchronized methods
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding the keyword "synchronized" to the method declaration is just a syntactic shortcut. Note that the following two are the same:
 
Ela Jha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This has lock on a single object(this) so all the methods which are synchronized cannot be accessed by another thread

It has lock on an object which can be defined by you let's say a String.
if u have four functions say put,get,push,pop & u want while pushing no thread shud be able to access pop but they can access put or get & vice versa then define single object for push & pop and another object for put & get.Remember lock is on an object
For Example
[/qb]<hr></blockquote>

[ edited to use separate code blocks rather than one big one - the text in between code was treated as an overly long line of code, messing up the display - Jim ]

[ July 17, 2006: Message edited by: Ela Jha ]
[ July 17, 2006: Message edited by: Jim Yingst ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The practical differences are in controlling scope and the monitor.

A synchronized method synchronizes the whole method (no surprise) while a block synchronizes only the block. You might have a longer method with some parts that need synchronized and others that don't. The block lets you sync only the exact lines that really need it.

A synchronized method synchronizes on the object instance or the class. These are very public monitors, meaning some other thread could synchronize on them for the wrong reason, leading to slowdowns or deadlocks. Maybe it's necessary to protect method A from multiple threads and method B from multiple threads, but it's ok if one thread is in A and another is in B. Synchronized methods can't do that, but blocks can by holding different object monitors.

Here's one way to synchronize on a private monitor:

Pop quiz: Why the normally wasteful "new String()" ?
[ July 17, 2006: Message edited by: Stan James ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stan]: Pop quiz: Why the normally wasteful "new String()" ?

Counterquiz: why a String here at all?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't think of a way to give any other Object a meaningful value. "new Anything()" would do the job, but "new Integer(1)" would make readers ask, "Why 1?" Hey, that could be real fun. Let's make it

new ServerSocket("123.234.012.123", 1234); // DO NOT CHANGE THIS ADDRESS!
[ July 17, 2006: Message edited by: Stan James ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I think the variable name is the only thing that's going to give it value in this context.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I would normally just use

as that raises a minimal number of irrelevant questions. In the unlikely event I need to print something out for debugging purposes, I'd rather have Object's toString(), which will at least print different values for different objects. If I have two different locks that are both Strings that say "lock", I would have a harder time telling them apart than with plain Objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic