java - Understanding synchronized blocks -
i have problem understanding why better use synchronized(syncobject)
synchronized(this)
. example, class:
public class pool implements objectpool { private object[] pool; private int initialcapacity; private int available = 0; private int waiting = 0; private final object syncobject = new object(); public pool(int initialcapacity) { this.initialcapacity = initialcapacity; pool = new object[initialcapacity]; } public void releaseobject(object o) throws exception { synchronized (syncobject) { pool[available] = o; available++; } if (waiting > 0) { notify(); } } }
because if use this
, thread trying execute method have wait, whereas if use object lock, restrict critical section.
Comments
Post a Comment