You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by jv...@apache.org on 2006/09/08 10:34:14 UTC

svn commit: r441434 - /directory/trunks/mina/core/src/main/java/org/apache/mina/management/StatCollector.java

Author: jvermillard
Date: Fri Sep  8 01:34:14 2006
New Revision: 441434

URL: http://svn.apache.org/viewvc?view=rev&rev=441434
Log:
fixed synchro issues and stoping the worker thread immedialty

Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/management/StatCollector.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/management/StatCollector.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/management/StatCollector.java?view=diff&rev=441434&r1=441433&r2=441434
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/management/StatCollector.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/management/StatCollector.java Fri Sep  8 01:34:14 2006
@@ -47,6 +47,13 @@
      */
     public static final String KEY = StatCollector.class.getName() + ".stat";
 
+    
+    /**
+     * @noinspection StaticNonFinalField
+     */
+    private static volatile int nextId = 0;
+    private final int id = nextId ++;
+    
     private final IoService service;
     private Worker worker;
     private int pollingInterval = 5000;
@@ -101,31 +108,35 @@
      */
     public void start()
     {
-        // TODO Make this method thread-safe.
-        if ( worker != null && worker.isAlive() )
-            throw new RuntimeException( "Stat collecting already started" );
-
-        // add all current sessions
-
-        polledSessions = new ArrayList();
-
-        for ( Iterator iter = service.getManagedServiceAddresses().iterator(); iter.hasNext(); )
+        synchronized (this) 
         {
-            SocketAddress element = ( SocketAddress ) iter.next();
-
-            for ( Iterator iter2 = service.getManagedSessions( element ).iterator(); iter2.hasNext(); )
+            if ( worker != null && worker.isAlive() )
+                throw new RuntimeException( "Stat collecting already started" );
+    
+            // add all current sessions
+    
+            polledSessions = new ArrayList();
+    
+            for ( Iterator iter = service.getManagedServiceAddresses().iterator(); iter.hasNext(); )
             {
-                addSession( ( IoSession ) iter2.next() );
-
+                SocketAddress element = ( SocketAddress ) iter.next();
+    
+                for ( Iterator iter2 = service.getManagedSessions( element ).iterator(); iter2.hasNext(); )
+                {
+                    addSession( ( IoSession ) iter2.next() );
+    
+                }
             }
-        }
 
-        // listen for new ones
-        service.addListener( serviceListener );
+            // listen for new ones
+            service.addListener( serviceListener );
+    
+            // start polling
+            worker = new Worker();
+            worker.start();
+
+        }
 
-        // start polling
-        worker = new Worker();
-        worker.start();
     }
 
     /**
@@ -133,26 +144,27 @@
      */
     public void stop()
     {
-        // TODO Make this method thread-safe.
-        // TODO Make the worker stop immediately, not waiting for the next loop.
-        service.removeListener( serviceListener );
-
-        // stop worker
-        worker.stop = true;
-        try
+        synchronized (this) 
         {
-            synchronized ( worker )
+            service.removeListener( serviceListener );
+
+            // stop worker
+            worker.stop = true;
+            worker.interrupt();
+            while( worker.isAlive() )
             {
-                worker.join();
+                try
+                {
+                    worker.join();
+                }
+                catch( InterruptedException e )
+                {
+                    //ignore since this is shutdown time
+                }
             }
-        }
-        catch ( InterruptedException e )
-        {
-            // ignore
-        }
-
-        polledSessions.clear();
 
+            polledSessions.clear();
+        }
     }
 
     /**
@@ -161,8 +173,10 @@
      */
     public boolean isRunning()
     {
-        // TODO Make this method thread-safe
-        return worker != null && worker.stop != true;
+        synchronized (this) 
+        {
+            return worker != null && worker.stop != true;
+        }
     }
 
     private void addSession( IoSession session )
@@ -184,8 +198,7 @@
 
         private Worker()
         {
-            // TODO Append a thread ID or any distingushed information to the thread name.
-            super( "StatCollectorWorker" );
+            super( "StatCollectorWorker-"+id );
         }
 
         public void run()
@@ -231,5 +244,4 @@
             }
         }
     }
-
-}
+}
\ No newline at end of file