mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r545136 - in /mina/branches/1.0/core/src: main/java/org/apache/mina/common/support/DefaultIoFuture.java test/java/org/apache/mina/common/support/FutureTest.java
Date Thu, 07 Jun 2007 10:02:46 GMT
Author: trustin
Date: Thu Jun  7 03:02:44 2007
New Revision: 545136

URL: http://svn.apache.org/viewvc?view=rev&rev=545136
Log:
Resolved issue: DIRMINA-377 (Reduce memory consumption of DefaultIoFuture)
* Minimized memory consumption by allocating a special member variable for the first listener.
* Applied additional (experimental) optimization to trunk

Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
    mina/branches/1.0/core/src/test/java/org/apache/mina/common/support/FutureTest.java

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java?view=diff&rev=545136&r1=545135&r2=545136
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
Thu Jun  7 03:02:44 2007
@@ -37,7 +37,8 @@
 {
     private final IoSession session;
     private final Object lock;
-    private final List listeners = new ArrayList();
+    private IoFutureListener firstListener;
+    private List otherListeners;
     private Object result;
     private boolean ready;
 
@@ -182,7 +183,14 @@
 
         synchronized( lock )
         {
-            listeners.add( listener );
+            if (firstListener == null) {
+                firstListener = listener;
+            } else {
+                if (otherListeners == null) {
+                    otherListeners = new ArrayList(1);
+                }
+                otherListeners.add(listener);
+            }
             if( ready )
             {
                 listener.operationComplete( this );
@@ -199,7 +207,13 @@
 
         synchronized( lock )
         {
-            listeners.remove( listener );
+            if (listener == firstListener) {
+                if (otherListeners != null && !otherListeners.isEmpty()) {
+                    firstListener = (IoFutureListener) otherListeners.remove(0);
+                }
+            } else if (otherListeners != null) {
+                otherListeners.remove(listener);
+            }
         }
     }
 
@@ -207,8 +221,13 @@
     {
         synchronized( lock )
         {
-            for( Iterator i = listeners.iterator(); i.hasNext(); ) {
-                ( ( IoFutureListener ) i.next() ).operationComplete( this );
+            if (firstListener != null) {
+                firstListener.operationComplete(this);
+                if (otherListeners != null) {
+                    for (Iterator i = otherListeners.iterator(); i.hasNext();) {
+                        ((IoFutureListener) i.next()).operationComplete( this );
+                    }
+                }
             }
         }
     }

Modified: mina/branches/1.0/core/src/test/java/org/apache/mina/common/support/FutureTest.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/test/java/org/apache/mina/common/support/FutureTest.java?view=diff&rev=545136&r1=545135&r2=545136
==============================================================================
--- mina/branches/1.0/core/src/test/java/org/apache/mina/common/support/FutureTest.java (original)
+++ mina/branches/1.0/core/src/test/java/org/apache/mina/common/support/FutureTest.java Thu
Jun  7 03:02:44 2007
@@ -26,6 +26,7 @@
 
 import org.apache.mina.common.IoFilterChain;
 import org.apache.mina.common.IoFuture;
+import org.apache.mina.common.IoFutureListener;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoService;
 import org.apache.mina.common.IoServiceConfig;
@@ -192,6 +193,100 @@
         assertFalse( future.isWritten() );
     }
     
+    public void testAddListener() throws Exception {
+        DefaultCloseFuture future = new DefaultCloseFuture( null );
+        assertFalse( future.isReady() );
+        assertFalse( future.isClosed() );
+        
+        TestListener listener1 = new TestListener();
+        TestListener listener2 = new TestListener();
+        future.addListener(listener1);
+        future.addListener(listener2);
+        
+        TestThread thread = new TestThread( future );
+        thread.start();
+        
+        future.setClosed();
+        thread.join();
+        
+        assertTrue( thread.success );
+        assertTrue( future.isReady() );
+        assertTrue( future.isClosed() );
+
+        assertSame( future, listener1.notifiedFuture );
+        assertSame( future, listener2.notifiedFuture );
+    }
+    
+    public void testLateAddListener() throws Exception {
+        DefaultCloseFuture future = new DefaultCloseFuture( null );
+        assertFalse( future.isReady() );
+        assertFalse( future.isClosed() );
+        
+        TestThread thread = new TestThread( future );
+        thread.start();
+        
+        future.setClosed();
+        thread.join();
+        
+        assertTrue( thread.success );
+        assertTrue( future.isReady() );
+        assertTrue( future.isClosed() );
+
+        TestListener listener = new TestListener();
+        future.addListener(listener);
+        assertSame( future, listener.notifiedFuture );
+    }
+    
+    public void testRemoveListener1() throws Exception {
+        DefaultCloseFuture future = new DefaultCloseFuture( null );
+        assertFalse( future.isReady() );
+        assertFalse( future.isClosed() );
+        
+        TestListener listener1 = new TestListener();
+        TestListener listener2 = new TestListener();
+        future.addListener(listener1);
+        future.addListener(listener2);
+        future.removeListener(listener1);
+        
+        TestThread thread = new TestThread( future );
+        thread.start();
+        
+        future.setClosed();
+        thread.join();
+        
+        assertTrue( thread.success );
+        assertTrue( future.isReady() );
+        assertTrue( future.isClosed() );
+
+        assertSame( null, listener1.notifiedFuture );
+        assertSame( future, listener2.notifiedFuture );
+    }
+    
+    public void testRemoveListener2() throws Exception {
+        DefaultCloseFuture future = new DefaultCloseFuture( null );
+        assertFalse( future.isReady() );
+        assertFalse( future.isClosed() );
+        
+        TestListener listener1 = new TestListener();
+        TestListener listener2 = new TestListener();
+        future.addListener(listener1);
+        future.addListener(listener2);
+        future.removeListener(listener2);
+        
+        TestThread thread = new TestThread( future );
+        thread.start();
+        
+        future.setClosed();
+        thread.join();
+        
+        assertTrue( thread.success );
+        assertTrue( future.isReady() );
+        assertTrue( future.isClosed() );
+
+        assertSame( future, listener1.notifiedFuture );
+        assertSame( null, listener2.notifiedFuture );
+    }
+    
     private static class TestThread extends Thread
     {
         private final IoFuture future;
@@ -205,6 +300,14 @@
         public void run()
         {
             success = future.join( 10000 );
+        }
+    }
+
+    private static class TestListener implements IoFutureListener {
+        private IoFuture notifiedFuture;
+        
+        public void operationComplete(IoFuture future) {
+            this.notifiedFuture = future;
         }
     }
 }



Mime
View raw message