Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized block  RSS feed

 
sridhar lakka
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks a lot in advance.
As per my knowledge we can apply synchronized key word to method or block to prevent dead lock situation.
But, please tell me after making synchronized where it will store exactly (heap/stack/RAM).
If serialized then where it will stores.

Regards,
Sree
 
Henry Wong
author
Sheriff
Posts: 22848
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If serialized then where it will stores.


Serialization and Synchronization are different operations.

Henry
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization aims to prevent race conditions/sharing violations, not deadlocks; it will in fact create a deadlock if misused.

Serialization allows objects to be written and read to/from streams, files, etc., but it does little more. Once an object is written to a stream, that stream can be pointed at a file, or output to screen or whatever have you, and conversely, the object can be read back from a stream.
 
Lal Mothi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all to achieve Multithreading mechanism in java we should go for synchronization. And this can be done in two ways depending on the requirement.

1. Synchronized block and
2. Synchronized method.

if you go for synchronized block it will lock a specific object.

if you go for synchronized method it will lock all the objects.

in other way Both the synchronized method and block are used to acquires the lock for an object. But the context may vary. Suppose if we want to invoke a critical method which is in a class whose access is not available then synchronized block is used. Otherwise synchronized method can be used.

Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc
 
Henry Wong
author
Sheriff
Posts: 22848
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Synchronized block and
2. Synchronized method.

if you go for synchronized block it will lock a specific object.

if you go for synchronized method it will lock all the objects.


Not quite. Synchronization of methods is just like a synchronized block, except that the JVM uses a particular object, and it is for the whole method. There is no locking of "all the objects".

For synchronization of instance methods, Java will use the "this" object. And for synchronization of static methods, Java will use the "class" object.

Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc


Not exactly sure what you are referring to here. Can you elaborate?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!