• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized block

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If serialized then where it will stores.



Serialization and Synchronization are different operations.

Henry
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You can't have everything. Where would you put it?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic