Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. using a private variable for the lock of synchronized block

using a private variable for the lock of synchronized block

Scheduled Pinned Locked Moved Java
databasealgorithmshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quartaela
    wrote on last edited by
    #1

    hi there i am trying to implement a simple chatserver and when searching for examples i saw a synchronized statement which uses a variable as a lock of the block.. the code is this;

    private List myList = new ArrayList();
    private Map();

    public void put( String s, Bar b ) {
    synchronized( myMap ) {
    myMap.put( s,b );
    // then some thing that may take a while like a database access or RPC or notifying listeners
    }
    }

    public void hasKey( String s, ) {
    synchronized( myMap ) {
    myMap.hasKey( s );
    }
    }

    public void add( Foo f ) {
    synchronized( myList ) {
    myList.add( f );
    // then some thing that may take a while like a database access or RPC or notifying listeners
    }
    }

    public Thing getMedianFoo() {
    Foo med = null;
    synchronized( myList ) {
    Collections.sort(myList);
    med = myList.get(myList.size()/2);
    }
    return med;
    }

    so i cant understand why and how can be a list be a lock. i always use "this" for accessing a synchronized statement. i appreciated if you can help me and thanks anyway : )

    N G 2 Replies Last reply
    0
    • Q quartaela

      hi there i am trying to implement a simple chatserver and when searching for examples i saw a synchronized statement which uses a variable as a lock of the block.. the code is this;

      private List myList = new ArrayList();
      private Map();

      public void put( String s, Bar b ) {
      synchronized( myMap ) {
      myMap.put( s,b );
      // then some thing that may take a while like a database access or RPC or notifying listeners
      }
      }

      public void hasKey( String s, ) {
      synchronized( myMap ) {
      myMap.hasKey( s );
      }
      }

      public void add( Foo f ) {
      synchronized( myList ) {
      myList.add( f );
      // then some thing that may take a while like a database access or RPC or notifying listeners
      }
      }

      public Thing getMedianFoo() {
      Foo med = null;
      synchronized( myList ) {
      Collections.sort(myList);
      med = myList.get(myList.size()/2);
      }
      return med;
      }

      so i cant understand why and how can be a list be a lock. i always use "this" for accessing a synchronized statement. i appreciated if you can help me and thanks anyway : )

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      You can lock any object. In principal, the lock object can only be accessed by the thread that took the lock. If you are accessing an object that is not thread safe, that can be called from multiple threads, then you should always lock that object.


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      1 Reply Last reply
      0
      • Q quartaela

        hi there i am trying to implement a simple chatserver and when searching for examples i saw a synchronized statement which uses a variable as a lock of the block.. the code is this;

        private List myList = new ArrayList();
        private Map();

        public void put( String s, Bar b ) {
        synchronized( myMap ) {
        myMap.put( s,b );
        // then some thing that may take a while like a database access or RPC or notifying listeners
        }
        }

        public void hasKey( String s, ) {
        synchronized( myMap ) {
        myMap.hasKey( s );
        }
        }

        public void add( Foo f ) {
        synchronized( myList ) {
        myList.add( f );
        // then some thing that may take a while like a database access or RPC or notifying listeners
        }
        }

        public Thing getMedianFoo() {
        Foo med = null;
        synchronized( myList ) {
        Collections.sort(myList);
        med = myList.get(myList.size()/2);
        }
        return med;
        }

        so i cant understand why and how can be a list be a lock. i always use "this" for accessing a synchronized statement. i appreciated if you can help me and thanks anyway : )

        G Offline
        G Offline
        gospidey
        wrote on last edited by
        #3

        the list used as a lock is an instance of class ArrayList. According to specification ArrayList implementation, an Arraylist is not synchroized, ie if multiple threads start accessing and or modifying the arraylist the state(data) of arraylist may get corrupted. So the instance "list" is used as lock so that block of code is synchronized and only one thread can access the list at any given point of time. do rate the answer or correct me if i am wrong. thanx Shreyas Rane

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups