Results 1 to 6 of 6

Thread: How do you synchronize java?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How do you synchronize java?

    Hi,
    I am working on java project in my company. While working on project yesterday our team leader asked me How do you synchronize java? I am not able to explain exactly how it works that's why I decided to taking your help. If you have any idea about this please help me.
    Thank you.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: How do you synchronize java?

    The java programming language supports two basic process one is synchronize method and other one is synchronize statement. If you want to make synchronize method just add synchronized keyword to it.

    Following is the example of it.


    Code:
     public class SynchronizedMethod
             {
            private int p = 0;
    
            public synchronized void incre() {
                p++;
            }
    
            public synchronized void decre() {
                p--;
            }
    
            public synchronized int value() {
                return p;
            }
        }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How do you synchronize java?

    synchronize is a type of command-line utility for doing one-way directory or for performing file synchronization. If source is match with the target then target is modified.synchronize process runs on any other Operating System if there is Java Runtime Environment. synchronize is free software that made from java.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How do you synchronize java?

    Two threads must not executed synchronized method simultaneously.

    like following example:
    synchronized ( Object1 )
    {
    /* Some important code */
    }

    Example of synchronized instance method:

    synchronized doityourself()
    {
    /*Some important code */
    }

    Example of synchronized syntactic shorthand:

    doityourself()
    {
    synchronized( this )
    {
    /* Some important code */
    }
    }


    Example of protecting static variables
    synchronized ( getClassdata() )
    {
    /*Some important code */
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How do you synchronize java?

    Synchronized Class Method:
    Code:
    class class1{
       static synchronized type MN1() {
          statement b
       }
    }
    Synchronized Instance Method:
    [CODE]

    class class2 {
    synchronized type MN2() {
    statement b
    }
    }




    Synchronized Statement:
    Code:
    class class3 {
       type MN3() {
          synchronized (obj1) {
             statement b
          }
       }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How do you synchronize java?

    Example of synchronize Methods :

    Code:
    public class synchronizeMethods
    {
       public synchronized void IM1 ()
       {
          important  code.
       }
    
       public synchronized void IM2 ()
       {
         important  code.
       }
    
       public static synchronized void CM ()
       {
          important  code.
       }
    }
    Code:
    public class synchronizeStatements
    {
       public void instanceMethod1 ()
       {
          //  code for setup
    
          synchronized (this)
          {
             // Update your file.
          }
    
          // code for cleanup.
       }
    
       public void IM2 ()
       {
          // code for setup
    
          synchronized (this)
          {
             // Read from this file.
          }
    
          // code for cleanup.
       }
    }

Similar Threads

  1. How to synchronize with Syncplicity
    By cyber-noob in forum Tips & Tweaks
    Replies: 2
    Last Post: 09-08-2010, 04:57 PM
  2. MP4's Audio is not able to Synchronize
    By Udbala in forum Portable Devices
    Replies: 3
    Last Post: 21-05-2010, 02:56 PM
  3. Synchronize an iterator in java
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 01:28 AM
  4. How to synchronize different mac mail
    By Zoe in forum Technology & Internet
    Replies: 2
    Last Post: 27-01-2009, 11:39 AM
  5. How to synchronize my Outlook?
    By aneesh in forum Windows Software
    Replies: 2
    Last Post: 08-01-2009, 02:18 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,714,022,231.63676 seconds with 16 queries