CHAPTER 11 (Web design company) REST-BASED MODEL VIEW CONTROLLER PATTERN

CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN 359 Implementing an Asynchronous Parent Interface Instance The big challenge with implementing an asynchronous Parent interface instance is managing the results. With the asynchronous Parent instance, multiple threads will be representing multiple local clients, and each local client will be generating results that need to be handed off to the controller. Synchronization is required when multiple Command instances hand off their results to the Parent instance that is running on a different thread to the local clients. Following is the implementation of the asynchronous class, which inherits from the previously defined ParentBase class: public class AsynchronousParent extends ParentBase { private LinkedList _results = new LinkedList(); public void addResult(Result result) { synchronized( _results) { _results.addLast( result); _results.notify(); } } public Object getResult() { synchronized( _results) { if( _results.size() > 0) { return _results.removeFirst(); } else { try { _results.wait(15000); } catch (InterruptedException e) { return null; } if( _results.size() > 0) { return _results.removeFirst(); } else { return null; } } } } public AsynchronousParent() { } } The data member _results represents the list used to manage the results handed to the controller from the executing local clients. The method addResultis used to add a result to the list, and getResult is used to retrieve a result. Both the adding and removing from the list is embedded in a synchronized keyword, where the synchronization object is the list itself. Using the synchronized function in this way ensures that only one thread is adding or removing a result from the list.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Leave a Reply