<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-141481265556432464</id><updated>2011-11-27T15:26:09.770-08:00</updated><category term='wait'/><category term='synchronization'/><category term='bottleneck'/><category term='bottle neck'/><category term='Thread.sleep()'/><category term='java'/><category term='thread dump'/><category term='monitor'/><title type='text'>Saurav Biswas</title><subtitle type='html'>Software Architecture, Cool Java Stuff and Web 2.0</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sauravbiswas.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sauravbiswas.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Saurav Biswas</name><uri>http://www.blogger.com/profile/16552867614339084789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-141481265556432464.post-7679616455819978055</id><published>2009-08-26T14:34:00.000-07:00</published><updated>2009-08-28T10:30:44.414-07:00</updated><title type='text'>Back and Forward Buttons working in RichFaces using Dojo</title><content type='html'>Recently I have been doing some work with RichFaces and encountered the biggest problem with AJAX based applications - the BACK button is not working. BTW, I cetainly recommend RichFaces, specifically its Ajax4JSF module, which make AJAX so simple. Anyways start doing some research and ultimately got working by integrating Dojo's back button support into my RichFaces application.&lt;br /&gt;&lt;br /&gt;Basically there are 3 part of the solution:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Adding the server side support to keep a history of the AJAX calls being made.&lt;/li&gt;&lt;li&gt;Add and initialize Dojo to your web application.&lt;/li&gt;&lt;li&gt;Send back button click events to load the previous state of the UI on the server side.&lt;/li&gt;&lt;/ol&gt;You can download the code from &lt;a href="http://download164.mediafire.com/lcjvhkpmzwyg/ei1gygwjlm2/rf.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So the first step is to create a session-scoped History bean that will maintain a queue of the states. Define 4 simple apis:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;addToBack - adds the previous state to the history&lt;/li&gt;&lt;li&gt;addToForward - adds the current state to the history&lt;/li&gt;&lt;li&gt;doBack - go to the previous state&lt;/li&gt;&lt;li&gt;doForward - go to the state ahead&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    public class HistoryBean {&lt;br /&gt;        private List&lt;state&gt; _backHistory = new ArrayList&lt;state&gt;();&lt;br /&gt;        private List&lt;state&gt; _forwardHistory = new ArrayList&lt;state&gt;();&lt;br /&gt;        public HistoryBean() {&lt;br /&gt;            super();&lt;br /&gt;        }&lt;br /&gt;        public void doBack() {&lt;br /&gt;            if (_backHistory.size() == 0) return;&lt;br /&gt;            State state = _backHistory.remove(0);&lt;br /&gt;            addToForward(state);&lt;br /&gt;        }&lt;br /&gt;        public void doForward() {&lt;br /&gt;            if (_forwardHistory.size() == 0) return;&lt;br /&gt;            State state = _forwardHistory.remove(0);&lt;br /&gt;            addToBack(state);&lt;br /&gt;        }&lt;br /&gt;        public void addToBack(State state) {&lt;br /&gt;            _backHistory.add(0, state);&lt;br /&gt;        }&lt;br /&gt;        public void addToForward(State state) {&lt;br /&gt;            _forwardHistory.add(0, state);&lt;br /&gt;        }&lt;br /&gt;        public State getCurrentState() {&lt;br /&gt;            if (_backHistory.size() == 0) return null;&lt;br /&gt;            State state = _backHistory.get(0);&lt;br /&gt;            return state;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Next add and intitialize the back button module from Dojo into your application web pages. You can find more information on Dojo and back button &lt;a href="http://www.dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/back-button/adding-your-page"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    &amp;lt;script type="text/javascript"  src="js/dojo/dojo.js"&lt;br /&gt;        djConfig="preventBackButtonFix: false"&amp;gt;&lt;br /&gt;    &amp;lt;/script&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;script type="text/javascript"&amp;gt;&lt;br /&gt;        // load the dojo.back package&lt;br /&gt;        dojo.require("dojo.back");&lt;br /&gt;        dojo.back.init();&lt;br /&gt;    &amp;lt;/script&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now create a state object to insert into the browser's history. The state provides 2 functions, one for back and the other for forward. Finally set the initial state when the page is first loaded.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    &amp;lt;script type="text/javascript"&amp;gt;&lt;br /&gt;       // define a state for the back button clicks&lt;br /&gt;       var state = {&lt;br /&gt;           back: function() {&lt;br /&gt;               invokeBack();&lt;br /&gt;           },&lt;br /&gt;           forward: function() {&lt;br /&gt;               invokeForward();&lt;br /&gt;           }&lt;br /&gt;       };&lt;br /&gt;       // load the initial state&lt;br /&gt;       dojo.back.setInitialState(state);&lt;br /&gt;   &amp;lt;/script&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now add two A4J based JavaScript functions that are invoked from the state functions above. These functions will changes the state at the server side when the back or forward buttons are clicked.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    &amp;lt;a4j:jsFunction name="invokeBack"&lt;br /&gt;        action="#{history.doBack}"&lt;br /&gt;        reRender="__my_tabs"/&amp;gt;&lt;br /&gt;    &amp;lt;a4j:jsFunction name="invokeForward"&lt;br /&gt;        action="#{history.doForward}"&lt;br /&gt;        reRender="__my_tabs"/&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;In these A4J javascript functions you are doing two things, first asking the server's session to switch to teh previous or next state in the queue. Secondly re-rendering the necessary components through the "reRender" attribute.&lt;br /&gt;&lt;br /&gt;That's all you need to get the back button functioning correctly in an application built Ajax4JSF/RichFaces. I am pretty sure you can apply the same to other JSF implementation like MyFaces and JSF RI to get it to work.&lt;br /&gt;&lt;br /&gt;You can download an example app that shows the back/forward button in action from &lt;a href="http://download164.mediafire.com/lcjvhkpmzwyg/ei1gygwjlm2/rf.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Things that you should be careful about in the server side history implementation.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Avoid loops in the state management. For example, when back in invoked it should call a setter that will call back into the history to add a back state.&lt;/li&gt;&lt;li&gt;Try to store objects with very small footprints in the state to conserve memory. You can also implement a FIFO model to remove very old back states.&lt;/li&gt;&lt;li&gt;Add an api like loadState() to the State interface (see sample code) that will allow you to implement the state loading in different parts of your application.&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/141481265556432464-7679616455819978055?l=sauravbiswas.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sauravbiswas.blogspot.com/feeds/7679616455819978055/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sauravbiswas.blogspot.com/2009/08/back-and-forward-buttons-working-in.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default/7679616455819978055'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default/7679616455819978055'/><link rel='alternate' type='text/html' href='http://sauravbiswas.blogspot.com/2009/08/back-and-forward-buttons-working-in.html' title='Back and Forward Buttons working in RichFaces using Dojo'/><author><name>Saurav Biswas</name><uri>http://www.blogger.com/profile/16552867614339084789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-141481265556432464.post-3026539534499851074</id><published>2009-08-17T13:38:00.000-07:00</published><updated>2009-08-17T14:14:14.028-07:00</updated><title type='text'>Securing methods using AOP</title><content type='html'>I am pretty sure some of us, the Java developers, have encountered this problem before. You would want to protect a particular method from illegal access. In other words no one should be able to right a Java program and be able to access this method. You in this world of software development all you code is already exposed, even though they might have published javadocs and any other form of documentation. Developers can use a decompiler to see the code and figure out how to call it. This is specifically a big problem for security related methods like encryption/decryption and password related apis.&lt;br /&gt;&lt;br /&gt;I hope I was able to explain the problem.&lt;br /&gt;&lt;br /&gt;Now to the solution. I used AOP (Aspect Oriented Programming) to solve the problem. AOP basically allows you to inject custom code into the compiled byte code of a class. So I take advantage of this really strong feature and implement a way to secure certain methods in the code base.&lt;br /&gt;&lt;br /&gt;Download AspectJ from &lt;a href="http://www.eclipse.org/aspectj/"&gt;http://www.eclipse.org/aspectj/&lt;/a&gt;. Create an aspect that protects the said API.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    public aspect ProtectMethodAspect {&lt;br /&gt;&lt;br /&gt;        private static Map allowedRegistry = new HashMap();&lt;br /&gt;&lt;br /&gt;        static {&lt;br /&gt;            List&lt;String&gt; allowedMethods = new ArrayList&lt;String&gt;();&lt;br /&gt;            allowedMethods.add("com.mycomp.security.PasswordUtil");&lt;br /&gt;            allowedMethods.add("com.mycomp.security.UserUtil");&lt;br /&gt;            allowedMethods.add("com.mycomp.somepkg.SomeClass");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        pointcut protectMethod() :&lt;br /&gt;            execution (* com.mycom.security.EncryptionUtil.decrypt(..));&lt;br /&gt;&lt;br /&gt;        before () : protectMethod() {&lt;br /&gt;&lt;br /&gt;            // get the caller by using the stack&lt;br /&gt;            try {&lt;br /&gt;                throw new Exception();&lt;br /&gt;            } catch (Exception e) {&lt;br /&gt;&lt;br /&gt;                StackTraceElement[] stack = e.getStackTrace();&lt;br /&gt;                String callingClass = stack[2].getClassName();&lt;br /&gt;&lt;br /&gt;                // if called within the same class, allow the call&lt;br /&gt;                if (callingClass.equals(thisJoinPoint.getSourceLocation()&lt;br /&gt;                                            .getWithinType().getName()))&lt;br /&gt;                    return;&lt;br /&gt;&lt;br /&gt;                String methodName = getMethodSignature(thisJoinPoint);&lt;br /&gt;&lt;br /&gt;                // check the caller is allowed to call&lt;br /&gt;                List allowedClasses = allowedRegistry.get(methodName);&lt;br /&gt;                if (allowedClasses != null) {&lt;br /&gt;                    // if not allowed the throw SecurityException&lt;br /&gt;                    if (!allowedClasses.contains(callingClass)) {&lt;br /&gt;                        throw new SecurityException("Access denied.");&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public static String getMethodSignature (JoinPoint jp) {&lt;br /&gt;            String mSig = jp.getSignature().toString();&lt;br /&gt;            int ppos = mSig.indexOf("(");&lt;br /&gt;            String mName = mSig.substring(0, ppos);&lt;br /&gt;            int spos = mName.indexOf(" ");&lt;br /&gt;            if (spos &gt; -1)&lt;br /&gt;                mName = mName.substring(spos+1);&lt;br /&gt;            return mName + mSig.substring(ppos);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now weave the aspect into the compiled classes under your build directory using the Ant task "iajc". For more information on this task refer to &lt;a href="http://www.eclipse.org/aspectj/doc/released/devguide/antTasks-iajc.html"&gt;http://www.eclipse.org/aspectj/doc/released/devguide/antTasks-iajc.html&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;When you try to access the method from any class that is not allowed you will get the exception "java.lang.SecurityException: Access Denied.".&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note: I know that this looks very impressive and you must be itching to do lot of other things with AOP but be careful. The biggest drawback of AOP is that it can easily get misused and lead to serious performance problems. You should only use AOP for targeted functionality and not to solve business problems.&lt;br /&gt;&lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/141481265556432464-3026539534499851074?l=sauravbiswas.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sauravbiswas.blogspot.com/feeds/3026539534499851074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sauravbiswas.blogspot.com/2009/08/securing-methods-using-aop.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default/3026539534499851074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default/3026539534499851074'/><link rel='alternate' type='text/html' href='http://sauravbiswas.blogspot.com/2009/08/securing-methods-using-aop.html' title='Securing methods using AOP'/><author><name>Saurav Biswas</name><uri>http://www.blogger.com/profile/16552867614339084789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-141481265556432464.post-4857607842644210178</id><published>2009-08-14T12:18:00.000-07:00</published><updated>2009-08-17T12:15:17.903-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='thread dump'/><category scheme='http://www.blogger.com/atom/ns#' term='synchronization'/><category scheme='http://www.blogger.com/atom/ns#' term='monitor'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='wait'/><category scheme='http://www.blogger.com/atom/ns#' term='bottleneck'/><category scheme='http://www.blogger.com/atom/ns#' term='Thread.sleep()'/><category scheme='http://www.blogger.com/atom/ns#' term='bottle neck'/><title type='text'>Synchronization: Java wait notify model using a monitor</title><content type='html'>One of the common problems that I have encountered in my coding career is synchronization bottle necks. This comes with using a Singleton design pattern. When one thread updates the objects and a second thread is trying to access the same set of objects you get into situations where there are stuck threads. You would want the second thread to wait till the first thread is finished updating the objects. The most common practice for java developers is use &lt;b&gt;Java Synchronization&lt;/b&gt;. For example:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    private static List&lt;string&gt; myObjects = new ArrayList&lt;string&gt;();&lt;br /&gt;&lt;br /&gt;    public static synchronized void putObject(String object) {&lt;br /&gt;        synchronized(myObjects) {&lt;br /&gt;            myObjects.add(object);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static String getObject(int index) {&lt;br /&gt;       synchronized(myObjects) {&lt;br /&gt;           return myObjects.get(index);&lt;br /&gt;       }&lt;br /&gt;    }&lt;br /&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The problem with this approach is that all the calls are serialized, meaning they will wait for the previous call to complete. To explain it more, say the update takes 100 milliseconds and each get takes 10 milliseconds each, you will see the following:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    putObject()      - started at 0 ms&lt;br /&gt;    getObject()[1]   - called at 1 ms&lt;br /&gt;    getObject()[2]   - called at 1 ms&lt;br /&gt;    getObject()[3]   - called at 1 ms&lt;br /&gt;    putObject()      - completed at 100 ms&lt;br /&gt;    getObject()[1]   - completed at 110 ms&lt;br /&gt;    getObject()[2]   - completed at 120 ms&lt;br /&gt;    getObject()[3]   - completed at 130 ms&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now you might say that why don't you remove the synchronized block from the getObject method. Obviously you can but that will lead to reading inconsistent data. In this case the getObject calls will not wait for the updates to complete and get the current data from the list, which might be invalid or stale. If you business requirement is fine with this situation you can most welcome to remove the synchronized block from the getObject method.&lt;br /&gt;&lt;br /&gt;But to make to that the getObject call do wait for the putObject call to finish, I use a wait-notify model. In this model all the thread making a getObject call will wait for the updateObject call to complete and then process with the getObject call without waiting for the other getObject calls to complete. To depict it in action, here is what you would see when you run the same test above:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    putObject()      - started at 0 ms&lt;br /&gt;    getObject()[1]   - called at 1 ms&lt;br /&gt;    getObject()[2]   - called at 1 ms&lt;br /&gt;    getObject()[3]   - called at 1 ms&lt;br /&gt;    putObject()      - completed at 100 ms&lt;br /&gt;    getObject()[1]   - completed at 110 ms&lt;br /&gt;    getObject()[2]   - completed at 110 ms&lt;br /&gt;    getObject()[3]   - completed at 110 ms&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;You must have noticed that all the getObject calls happen at the same time at 110 ms and not 10 ms apart.&lt;br /&gt;&lt;br /&gt;So how do I achieve this? Using a simple monitor object. This object has a simple boolen "isBusy" flag which is used to control whether a thread is allowed to proceed or not. Here is the code for the monitor implementation:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    public class Monitor {&lt;br /&gt;&lt;br /&gt;        private boolean isBusy = false;&lt;br /&gt;&lt;br /&gt;        public synchronized void lock() {&lt;br /&gt;            // locks the monitor&lt;br /&gt;            while (true) {&lt;br /&gt;                if (isBusy) {&lt;br /&gt;                    try {&lt;br /&gt;                        wait();&lt;br /&gt;                    } catch (InterruptedException e) {&lt;br /&gt;                        // ignore&lt;br /&gt;                    }&lt;br /&gt;                } else  {&lt;br /&gt;                    break;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            isBusy = true;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void checkLock() {&lt;br /&gt;            while (true) {&lt;br /&gt;                if (isBusy) {&lt;br /&gt;                    synchronized(this) {&lt;br /&gt;                        try {&lt;br /&gt;                            wait();&lt;br /&gt;                        } catch (InterruptedException e) {&lt;br /&gt;                            // ignore&lt;br /&gt;                        }&lt;br /&gt;                    }&lt;br /&gt;                } else  {&lt;br /&gt;                    break;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public synchronized void releaseLock() {&lt;br /&gt;            isBusy = false;&lt;br /&gt;            this.notifyAll(); // this will release the lock&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;And this is the implementation of the putObject:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    private static Monitor monitor = new Monitor();&lt;br /&gt;&lt;br /&gt;    public static synchronized void putObject(String object) {&lt;br /&gt;        monitor.lock(); // this will lock the monitor&lt;br /&gt;        try {&lt;br /&gt;            myObjects.add(object);&lt;br /&gt;        } finally {&lt;br /&gt;            monitor.releaseLock(); // this will release lock on the monitor&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;And this is the implementation of the getObject:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid rgb(153, 153, 153); background-color: rgb(221, 221, 221); font-family: courier new; font-size: 12px;"&gt;&lt;pre&gt;    private static Monitor monitor = new Monitor();&lt;br /&gt;&lt;br /&gt;    public static String getObject(int index) {&lt;br /&gt;        monitor.checkLock(); // this will check for a lock on the monitor&lt;br /&gt;        return myObjects.get(index);&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The notifyAll() call in the releaseLonc() methos will notify all thread waiting on the monitor that the lock has been released, which will allow all the thread to proceed with the execution at the same time.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/141481265556432464-4857607842644210178?l=sauravbiswas.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sauravbiswas.blogspot.com/feeds/4857607842644210178/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sauravbiswas.blogspot.com/2009/08/synchronization-java-wait-notify-model.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default/4857607842644210178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/141481265556432464/posts/default/4857607842644210178'/><link rel='alternate' type='text/html' href='http://sauravbiswas.blogspot.com/2009/08/synchronization-java-wait-notify-model.html' title='Synchronization: Java wait notify model using a monitor'/><author><name>Saurav Biswas</name><uri>http://www.blogger.com/profile/16552867614339084789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
