[archstudio-commits] svn commit: r7078 - in /bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4: AsyncModelEventProcessor.java DefaultBNAModel.java DefaultBNAWorld.java

archstudio-commits-owner at uci.edu archstudio-commits-owner at uci.edu
Sat May 3 20:22:15 PDT 2008


Author: shendric
Date: Sat May  3 20:22:15 2008
New Revision: 7078

Log:
Added ability to profile event processing

Removed:
    bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/AsyncModelEventProcess=
or.java
Modified:
    bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAModel.java
    bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAWorld.java

Modified: bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAModel.=
java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAModel.java (=
original)
+++ bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAModel.java S=
at May  3 20:22:15 2008
@@ -5,6 +5,9 @@
 import java.util.ListIterator;
 import java.util.Map;
 import java.util.WeakHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
 =

 import edu.uci.isr.bna4.BNAModelEvent.EventType;
@@ -13,10 +16,13 @@
 import edu.uci.isr.sysutils.ThreadEventsLock;
 =

 public class DefaultBNAModel
-    implements IBNAModel, IThingListener, IEventListener<BNAModelEvent>{
+	implements IBNAModel, IThingListener, IEventListener<BNAModelEvent>{
+
+	private static final boolean DEBUG =3D false;
+	private static final String format =3D "%-75s : %,14d\n";
 =

 	static class ThingIndex
-	    implements IBNASynchronousLockModelListener{
+		implements IBNASynchronousLockModelListener{
 =

 		protected final Map<String, IThing> indexMap =3D new WeakHashMap<String,=
 IThing>();
 =

@@ -39,11 +45,21 @@
 	protected final ThreadEventsLock<BNAModelEvent> modelEventLock =3D new Th=
readEventsLock<BNAModelEvent>();
 	protected final ListenerList<IBNASynchronousLockModelListener> syncLockLi=
steners =3D new ListenerList<IBNASynchronousLockModelListener>(IBNASynchron=
ousLockModelListener.class);
 	protected final ListenerList<IBNASynchronousModelListener> syncListeners =
=3D new ListenerList<IBNASynchronousModelListener>(IBNASynchronousModelList=
ener.class);
-	protected final AsyncModelEventProcessor asyncModelEventProcessor =3D new=
 AsyncModelEventProcessor();
+	protected final ListenerList<IBNAModelListener> asyncListeners =3D new Li=
stenerList<IBNAModelListener>(IBNAModelListener.class);
+	protected final ExecutorService asyncExecutor;
 =

 	public DefaultBNAModel(){
 		syncLockListeners.add(thingIndex);
 		modelEventLock.addEventListener(this);
+		asyncExecutor =3D Executors.newSingleThreadExecutor();
+	}
+
+	public void terminate(boolean t){
+		try{
+			asyncExecutor.awaitTermination(5, TimeUnit.SECONDS);
+		}
+		catch(InterruptedException ie){
+		}
 	}
 =

 	public Lock getLock(){
@@ -51,11 +67,11 @@
 	}
 =

 	public void addBNAModelListener(IBNAModelListener l){
-		asyncModelEventProcessor.addModelListener(l);
+		asyncListeners.add(l);
 	}
 =

 	public void removeBNAModelListener(IBNAModelListener l){
-		asyncModelEventProcessor.removeModelListener(l);
+		asyncListeners.remove(l);
 	}
 =

 	public void addSynchronousBNAModelListener(IBNASynchronousModelListener l=
){
@@ -74,26 +90,66 @@
 		syncLockListeners.remove(l);
 	}
 =

-	public void handleEvent(BNAModelEvent evt){
+	public void handleEvent(final BNAModelEvent evt){
 		for(IBNASynchronousModelListener l: syncListeners.getListeners()){
 			try{
+				long lTime;
+				if(DEBUG){
+					lTime =3D System.nanoTime();
+				}
 				l.bnaModelChangedSync(evt);
+				if(DEBUG){
+					lTime =3D System.nanoTime() - lTime;
+					System.err.printf(format, "S:" + l, lTime);
+				}
 			}
 			catch(Throwable t){
 				t.printStackTrace();
 			}
 		}
-		asyncModelEventProcessor.bnaModelChangedSync(evt);
-		if(evt.eventType =3D=3D BNAModelEvent.EventType.THING_REMOVING){
-			_removeThing(evt.targetThing);
-		}
+
+		final IBNAModelListener[] listenersAtTimeOfEvent =3D asyncListeners.getL=
isteners();
+		asyncExecutor.submit(new Runnable(){
+
+			public void run(){
+				for(IBNAModelListener l: listenersAtTimeOfEvent){
+					try{
+						long lTime;
+						if(DEBUG){
+							lTime =3D System.nanoTime();
+						}
+						l.bnaModelChanged(evt);
+						if(DEBUG){
+							lTime =3D System.nanoTime() - lTime;
+							System.err.printf(format, "A:" + l, lTime);
+						}
+					}
+					catch(Throwable t){
+						t.printStackTrace();
+					}
+					finally{
+						if(evt.eventType =3D=3D BNAModelEvent.EventType.THING_REMOVING){
+							_removeThing(evt.targetThing);
+						}
+					}
+				}
+			}
+		});
 	}
 =

 	protected void fireBNAModelEvent(BNAModelEvent evt){
 		assert modelEventLock.isHeldByCurrentThread();
 		for(IBNASynchronousLockModelListener l: syncLockListeners.getListeners()=
){
 			try{
+				long lTime;
+				if(DEBUG){
+					lTime =3D System.nanoTime();
+				}
 				l.bnaModelChangedSyncLock(evt);
+				if(DEBUG){
+					lTime =3D System.nanoTime() - lTime;
+					System.err.printf(format, "L:" + l, lTime);
+				}
 			}
 			catch(Throwable t){
 				t.printStackTrace();

Modified: bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAWorld.=
java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAWorld.java (=
original)
+++ bna4/trunk/edu.uci.isr.bna4/src/edu/uci/isr/bna4/DefaultBNAWorld.java S=
at May  3 20:22:15 2008
@@ -1,7 +1,10 @@
 package edu.uci.isr.bna4;
 =

 public class DefaultBNAWorld
-    implements IBNAWorld, IBNAModelListener, IBNASynchronousModelListener,=
 IBNASynchronousLockModelListener{
+	implements IBNAWorld, IBNAModelListener, IBNASynchronousModelListener, IB=
NASynchronousLockModelListener{
+
+	private static final boolean DEBUG =3D false;
+	private static final String format =3D "%-75s : %,14d\n";
 =

 	protected String id =3D null;
 	protected IBNAModel model =3D null;
@@ -21,19 +24,43 @@
 =

 	public void bnaModelChanged(BNAModelEvent evt){
 		for(IBNAModelListener logic: logicManager.getThingLogics(IBNAModelListen=
er.class)){
+			long lTime;
+			if(DEBUG){
+				lTime =3D System.nanoTime();
+			}
 			logic.bnaModelChanged(evt);
+			if(DEBUG){
+				lTime =3D System.nanoTime() - lTime;
+				System.err.printf(format, "A:" + logic, lTime);
+			}
 		}
 	}
 =

 	public void bnaModelChangedSync(BNAModelEvent evt){
 		for(IBNASynchronousModelListener logic: logicManager.getThingLogics(IBNA=
SynchronousModelListener.class)){
+			long lTime;
+			if(DEBUG){
+				lTime =3D System.nanoTime();
+			}
 			logic.bnaModelChangedSync(evt);
+			if(DEBUG){
+				lTime =3D System.nanoTime() - lTime;
+				System.err.printf(format, "S:" + logic, lTime);
+			}
 		}
 	}
 =

 	public void bnaModelChangedSyncLock(BNAModelEvent evt){
 		for(IBNASynchronousLockModelListener logic: logicManager.getThingLogics(=
IBNASynchronousLockModelListener.class)){
+			long lTime;
+			if(DEBUG){
+				lTime =3D System.nanoTime();
+			}
 			logic.bnaModelChangedSyncLock(evt);
+			if(DEBUG){
+				lTime =3D System.nanoTime() - lTime;
+				System.err.printf(format, "L:" + logic, lTime);
+			}
 		}
 	}
 =





More information about the archstudio-commits mailing list