| ||||
| ||||
| ||||
Code - Class EDU.oswego.cs.dl.util.concurrent.ObservableSync1 /* 2 File: ObservableSync.java 3 4 Originally written by Doug Lea and released into the public domain. 5 This may be used for any purposes whatsoever without acknowledgment. 6 Thanks for the assistance and support of Sun Microsystems Labs, 7 and everyone contributing, testing, and using this code. 8 9 History: 10 Date Who What 11 1Aug1998 dl Create public version 12 */ 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 import java.util.*; 16 17 /** 18 * The ObservableSync class performs no synchronization 19 * itself, but invokes event-style messages to other 20 * observer objects upon invocation of Sync methods. 21 * These observers typically perform monitoring, logging, 22 * or other bookkeeping operations surrounding the object 23 * being managed by this Sync object. 24 * <p> 25 * Because ObservableSync does not itself perform any synchronization 26 * control, the attempt operation always succeeds. 27 * This class is typically used (via LayeredSync) as a wrapper 28 * around those that do perform synchronization control. 29 * <p> 30 * This class is based around a standard Observer design pattern. 31 * It is not hard to convert this to instead use a Listener 32 * design (as seen in AWT and JavaBeans), by defining associated 33 * EventObjects and forwarding them. 34 * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] 35 * @see LayeredSync 36 **/ 37 38 39 public class ObservableSync implements Sync { 40 41 42 /** 43 * Interface for objects that observe ObservableSyncs. 44 **/ 45 public interface SyncObserver { 46 /** 47 * Method called upon acquire or successful attempt of Sync 48 **/ 49 50 public void onAcquire(Object arg); 51 52 /** 53 * Method called upon release of Sync. 54 **/ 55 public void onRelease(Object arg); 56 } 57 58 protected final CopyOnWriteArraySet observers_ = new CopyOnWriteArraySet(); 59 protected Object arg_; 60 61 /** 62 * Create an ObservableSync that uses the supplied argument 63 * for all notifications. The argument is typically an 64 * object that is being managed by this Sync object. 65 **/ 66 67 public ObservableSync(Object notificationArgument) { 68 arg_ = notificationArgument; 69 } 70 71 /** 72 * Return the argument used for notifications 73 **/ 74 public synchronized Object getNotificationArgument() { 75 return arg_; 76 } 77 78 /** 79 * Set the argument used for notifications. 80 * @return the previous value of this argument 81 **/ 82 83 public synchronized Object setNotificationArgument(Object notificationArg) { 84 Object old = arg_; 85 arg_ = notificationArg; 86 return old; 87 } 88 89 90 91 public void acquire() { 92 Object arg = getNotificationArgument(); 93 for (Iterator it = observers_.iterator(); it.hasNext(); ) { 94 ((SyncObserver)it.next()).onAcquire(arg); 95 } 96 } 97 98 public boolean attempt(long msecs) { 99 acquire(); 100 return true; 101 } 102 103 public void release() { 104 Object arg = getNotificationArgument(); 105 for (Iterator it = observers_.iterator(); it.hasNext(); ) { 106 ((SyncObserver)it.next()).onRelease(arg); 107 } 108 } 109 110 111 112 /** Add obs to the set of observers **/ 113 public void attach(SyncObserver obs) { 114 observers_.add(obs); 115 } 116 117 /** Remove obs from the set of observers. No effect if not in set **/ 118 public void detach(SyncObserver obs) { 119 observers_.remove(obs); 120 } 121 122 /** Return an iterator that can be used to traverse through 123 * current set of observers 124 **/ 125 126 public Iterator observers() { 127 return observers_.iterator(); 128 } 129 130 131 } 132 133 134 |
||||
Bookmark
|digg
|del.icio.us
|furl
|spurl
|blinklist
|reddit
|yahoo
|blinkbits
|blogmarks
|de.lirio.us
|smarking
| | ||||
![]() | ||||
Java API By Example, From Geeks To Geeks. |
Conditions of Use |
About Us
© 2002 - 2005, KickJava.com, or its affiliates
|