mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r545134 - in /mina/branches/1.1/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:24 GMT
Author: trustin
Date: Thu Jun  7 03:02:21 2007
New Revision: 545134

URL: http://svn.apache.org/viewvc?view=rev&rev=545134
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.1/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
    mina/branches/1.1/core/src/test/java/org/apache/mina/common/support/FutureTest.java

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java?view=diff&rev=545134&r1=545133&r2=545134
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/common/support/DefaultIoFuture.java
Thu Jun  7 03:02:21 2007
@@ -36,7 +36,8 @@
 {
     private final IoSession session;
     private final Object lock;
-    private final List<IoFutureListener> listeners = new ArrayList<IoFutureListener>();
+    private IoFutureListener firstListener;
+    private List<IoFutureListener> otherListeners;
     private Object result;
     private boolean ready;
 
@@ -160,7 +161,14 @@
 
         synchronized( lock )
         {
-            listeners.add( listener );
+            if (firstListener == null) {
+                firstListener = listener;
+            } else {
+                if (otherListeners == null) {
+                    otherListeners = new ArrayList<IoFutureListener>(1);
+                }
+                otherListeners.add(listener);
+            }
             if( ready )
             {
                 listener.operationComplete( this );
@@ -177,7 +185,13 @@
 
         synchronized( lock )
         {
-            listeners.remove( listener );
+            if (listener == firstListener) {
+                if (otherListeners != null && !otherListeners.isEmpty()) {
+                    firstListener = otherListeners.remove(0);
+                }
+            } else if (otherListeners != null) {
+                otherListeners.remove(listener);
+            }
         }
     }
 
@@ -185,8 +199,13 @@
     {
         synchronized( lock )
         {
-            for (IoFutureListener l : listeners) {
-                l.operationComplete( this );
+            if (firstListener != null) {
+                firstListener.operationComplete(this);
+                if (otherListeners != null) {
+                    for (IoFutureListener l : otherListeners) {
+                        l.operationComplete( this );
+                    }
+                }
             }
         }
     }

Modified: mina/branches/1.1/core/src/test/java/org/apache/mina/common/support/FutureTest.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/test/java/org/apache/mina/common/support/FutureTest.java?view=diff&rev=545134&r1=545133&r2=545134
==============================================================================
--- mina/branches/1.1/core/src/test/java/org/apache/mina/common/support/FutureTest.java (original)
+++ mina/branches/1.1/core/src/test/java/org/apache/mina/common/support/FutureTest.java Thu
Jun  7 03:02:21 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;
@@ -194,6 +195,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;
@@ -208,6 +303,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