import java.beans.*; import utilities.*; import utilities.beans.*; public class MTListeners extends Thread implements PropertyChangeListener { protected UnexpectedEvents myBean; // the target bean to bash.. protected int myID; // each thread carries a bit of ID protected boolean readyForEvents = false; // have we registered yet ? //------------------------------------------------------------------- // main() entry point //------------------------------------------------------------------- public static void main (String[] args) { UnexpectedEvents bean; Thread thread; bean = (UnexpectedEvents) BeansKit.newBean("UnexpectedEvents"); for (int i=0; i < 5; i++) { // start N threads to bash bean thread = new MTListeners(bean, i); // threads get access to bean thread.start(); } } //------------------------------------------------------------------- // MTListeners Constructor //------------------------------------------------------------------- public MTListeners(UnexpectedEvents bean, int id) { this.myBean = bean; // note the bean to address this.myID = id; // note who we are } //------------------------------------------------------------------- // the thread main loop: // do forever // register as listener, set flag to accept events // deregister as listener, set flag to refuse events // change bean property // // if we ever get an event while we shouldn't get one, say so and halt //------------------------------------------------------------------- public void run() { while ( true ) { // register with bean, this means we might expect events from now // on. synchronized (this) { System.out.println(myID +" registering."); myBean.addPropertyChangeListener(this); readyForEvents = true; } // let other threads run for a bit randomSleep(); // now deregister ourselves with bean, meaning we should not // receive any more events from bean from now on. synchronized (this) { System.out.println(myID +" un-registering."); myBean.removePropertyChangeListener(this); readyForEvents = false; } // let other threads run for a bit randomSleep(); // then modify the bean's property: System.out.println(myID +" about to change bean property.."); myBean.setProperty( (int) (Math.random()*50)); } } //------------------------------------------------------------------- // the implementation of the PropertyChangeListener interface //------------------------------------------------------------------- public void propertyChange(PropertyChangeEvent pcEvent) { if ( readyForEvents == false ) { System.out.println("I ("+ myID +") got an event while I did not" + " expect to receive one!!"); System.exit(0); } System.out.println(myID +" heard of property change."); } //------------------------------------------------------------------- // utility function to put a thread to sleep for a random period of // less than 1 second. //------------------------------------------------------------------- protected void randomSleep() { MiscKit.delay( (int) (Math.random()*1000)); } } // End of Class MTListeners