Results 1 to 3 of 3

Thread: What is synchronization in Java And why it is important?

  1. #1
    Join Date
    Oct 2008
    Posts
    33

    What is synchronization in Java And why it is important?

    Hi guys,
    Can anyone tell me what synchronization does in java and how it is implemented, and it is important?

    please Help.

  2. #2
    Join Date
    Mar 2008
    Posts
    382
    Synchronization is best use with the Multi-Threading in Java, Synchronization is the capability to control the access of multiple threads to share resources. Without synchronization it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value.This often leads to an error.

    Example for Synchronization
    public synchronized void enqueue(Object item)
    {
    // Body of method goes here

    }

    shorthand notation for
    public void enqueue(Object item)
    {
    synchronized (this)
    {
    //Body of method goes here
    }
    }

    Hope this will give idea about synchronization and its code implementation.

  3. #3
    Join Date
    Dec 2007
    Posts
    1,599
    The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements.
    To make a method synchronized, simply add the synchronized keyword to its declaration:
    public class SynchronizedCounter
    {
    private int c = 0;
    public synchronized void increment()
    {
    c++;
    }
    public synchronized void decrement()
    {
    c--;
    }
    public synchronized int value()
    {
    return c;
    }
    }

    If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:

    * First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
    * Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

Similar Threads

  1. WI-FI synchronization in IOS 5
    By Ashokvardan in forum Portable Devices
    Replies: 10
    Last Post: 27-10-2011, 03:20 PM
  2. Replies: 3
    Last Post: 07-08-2010, 12:33 PM
  3. Problem with Synchronization in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 11:31 AM
  4. LAN games with Synchronization
    By Jasonholt in forum Video Games
    Replies: 7
    Last Post: 07-04-2009, 11:12 AM
  5. Synchronization of databases
    By Dadhij in forum Software Development
    Replies: 4
    Last Post: 15-12-2008, 07:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,388,871.42075 seconds with 16 queries