home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 6.4 KB | 200 lines |
- /*
- * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or modify this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur application, except a utility or class of similar
- * nature to this product. You may distribute this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package symantec.itools.db.awt.genutil;
-
- import java.util.*;
-
- /**
- * Class encapsulates a callback to an object.
- */
- class NotifyPackage {
- Notifiable target;
- Vector args = new Vector();
-
- NotifyPackage(Notifiable n) {
- this(n, null);
- }
-
- NotifyPackage(Notifiable n, Object o) {
- target = n;
- addArg(o);
- }
-
- final void addArg(Object arg) {
- args.addElement(arg);
- }
-
- final void asyncNotify() {
- //catch all exceptions so don't end thread
- try {
- if( target!=null){
- for(int i=0;i<args.size();i++)
- {
- if(args.elementAt(i)==null)
- {
- args.removeAllElements();
- /*<vj>*/
- //return;
- break;
- /*<vj>*/
-
- }
- }
- target.asyncNotify(args);
- }
- } catch(Exception ex) {
- System.err.println("Error occurred during asynchronous notification");
- ex.printStackTrace();
- }
- args.removeAllElements();
-
-
- }
- }
-
- /**
- * Class provides the ability for classes to implement asynchronously executed
- * methods. Classes usually create their own thread to perform asynchronous
- * code, but the problem of proliferating the number of threads active within
- * a virtual machine. The more threads present, the more overhead and less
- * efficiently it runs.
- * <p>
- * This class could be used to perform offscreen drawing in a separate thread,
- * simulate user events, or perform background tasks. Note: Only one thread
- * is created for the entire virtual machine so blocking calls should not
- * be performed within the context of the callback thread, for this would
- * block all other objects from getting notified.
- * <p>
- * Objects to be called back must implement the Notifiable interface.
- */
- public class AsyncNotifier extends Thread {
- /**
- * The objects that need to be notified.
- */
- Hashtable toBeNotified = new Hashtable();
-
- /**
- * The actual notifier within a virtual machine.
- */
- private static AsyncNotifier notifier = new AsyncNotifier();
-
- /**
- * Private contructor used to construct the one and only notifier.
- */
- private AsyncNotifier() {
- setPriority(6);
- start();
- }
-
- /**
- * Class method used to request an asynchronous callback to an object.
- * @param n The object to be notfied.
- */
- public static void notify(Notifiable n) {
- notifier.notifyObject(n, null);
- }
-
- /**
- * Class method used to request an asynchronous callback to an object.
- * When the object is notified, a context argument is passed to it.
- * @param n The object to be notfied.
- * @param arg The context argument (object/class specific).
- *
- */
- public static void notify(Notifiable n, Object arg) {
- notifier.notifyObject(n, arg);
- }
-
- /**
- * The internal function used to actually register the object for a callback.
- */
- private final void notifyObject(Notifiable n) {
- notifyObject(n, null);
- }
-
- /**
- * The internal function used to actually register the object for a callback
- * using a context argument.
- */
- private final synchronized void notifyObject(Notifiable n, Object arg) {
- NotifyPackage p;
- p = (NotifyPackage)toBeNotified.get(n);
- if (p == null) {
- p = new NotifyPackage(n, arg);
- toBeNotified.put(n, p);
- }
-
- p.addArg(arg);
- notify();
- }
-
- /**
- * The notifiers body.
- */
- public void run() {
- while(true) {
- doNotifications();
- try {
- synchronized(this) {
- if (toBeNotified.size() == 0) {
- wait();
- } else {
- //yeild to let other threads run
- Thread.sleep(0);
- }
- }
- } catch(Exception ex) {}
- }
- }
-
- /**
- * Method iterates the objects to be notified and triggers the callbacks.
- */
- private void doNotifications() {
- Hashtable ht;
- synchronized (this) {
- ht = (Hashtable)toBeNotified.clone();
- toBeNotified.clear();
- }
-
- Enumeration e = ht.elements();
- NotifyPackage p;
-
- while (e.hasMoreElements()) {
- p = (NotifyPackage)e.nextElement();
- toBeNotified.remove(p.target);
- p.asyncNotify();
- }
- }
- }
-
-