• 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

Whats does Synchronised() mean?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can any one tell me what does the below code mean?

Synchonized(ClassName.class){

// Piece of code

}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This synchronizes the block of code on the class object of class ClassName.

See Synchronization in Oracle's Java Tutorials to learn about thread synchronization.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below mentioned Synchronization syntax helps to set the block code executed at once.

Synchonized(ClassName.class){

// Piece of code

}

We can realize the usage of this when we are going for Thread or Singleton.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dinesh Kumar Babu wrote:The below mentioned Synchronization syntax helps to set the block code executed at once.


I don't know what exactly you mean be "executed at once". With synchronization you can make sure that only one thread at a time can execute a block of code. But it doesn't mean that the block of code is not going to be interrupted by thread scheduling.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Dinesh Kumar Babu
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful about spellings: you have written Synchronised() and Synchonized(), but the correct spelling of the keyword is synchronized(). Yes, the difference is significant.
 
Shankara Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please below link for how synchronization is used in java.

Synchronization

 
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have multiple threads operating on the same state, you need to make sure that the state of the object is not corrupted.

The only way to achieve it is to make your method / block as synchronized.

At any point of time only one thread is allowed to execute the shared resource (method/block).

There are 2 types of locks

Object lock

If your shared resorce is the instance method/block then every thread has to acquire lock before execting.

Object lock - complete method



Object lock - method block



Class lock

If you want to make your class/static method shared resource then you can do it in 2 ways.

Class lock - complete method



Class lock - method block



Regards,
Ramakrishna Gutha
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good answer, Ramakrishna.

But there aren't really two fundamentally different kinds of locks. Each Java object has a lock that can be used for thread synchronization. What you call a "class lock" is a lock on the java.lang.Class object of a class. (For each class, there is a corresponding java.lang.Class object). When you synchronize a static method, you're locking the java.lang.Class object of the class that the method is in.
 
reply
    Bookmark Topic Watch Topic
  • New Topic