felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1763359 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework: Felix.java StatefulResolver.java util/FelixConstants.java
Date Wed, 05 Oct 2016 08:14:06 GMT
Author: gnodet
Date: Wed Oct  5 08:14:06 2016
New Revision: 1763359

URL: http://svn.apache.org/viewvc?rev=1763359&view=rev
Log:
[FELIX-5247] Reduce number of threads created by Resolver during a startup of OSGi-based applications

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=1763359&r1=1763358&r2=1763359&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Wed Oct  5 08:14:06
2016
@@ -4961,6 +4961,8 @@ public class Felix extends BundleImpl im
             m_cache.release();
             m_cache = null;
 
+            m_resolver.stop();
+
             // Set the framework state to resolved.
             acquireBundleLock(Felix.this, Bundle.STOPPING);
             try

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java?rev=1763359&r1=1763358&r2=1763359&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java Wed
Oct  5 08:14:06 2016
@@ -31,11 +31,19 @@ import java.util.Map.Entry;
 import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.felix.framework.capabilityset.CapabilitySet;
 import org.apache.felix.framework.capabilityset.SimpleFilter;
 import org.apache.felix.framework.resolver.CandidateComparator;
 import org.apache.felix.framework.resolver.ResolveException;
+import org.apache.felix.framework.util.FelixConstants;
 import org.apache.felix.framework.util.ShrinkableCollection;
 import org.apache.felix.framework.util.Util;
 import org.apache.felix.framework.util.manifestparser.NativeLibrary;
@@ -70,6 +78,7 @@ class StatefulResolver
     private final Logger m_logger;
     private final Felix m_felix;
     private final ServiceRegistry m_registry;
+    private final Executor m_executor;
     private final ResolverImpl m_resolver;
     private boolean m_isResolving = false;
 
@@ -93,7 +102,8 @@ class StatefulResolver
         m_felix = felix;
         m_registry = registry;
         m_logger = m_felix.getLogger();
-        m_resolver = new ResolverImpl(m_logger);
+        m_executor = getExecutor();
+        m_resolver = new ResolverImpl(m_logger, m_executor);
 
         m_revisions = new HashSet<BundleRevision>();
         m_fragments = new HashSet<BundleRevision>();
@@ -119,14 +129,67 @@ class StatefulResolver
         m_capSets.put(BundleRevision.HOST_NAMESPACE,  new CapabilitySet(indices, true));
     }
 
+    private Executor getExecutor()
+    {
+        String str = m_felix.getProperty(FelixConstants.RESOLVER_PARALLELISM);
+        int parallelism = Runtime.getRuntime().availableProcessors();
+        if (str != null)
+        {
+            try
+            {
+                parallelism = Integer.parseInt(str);
+            }
+            catch (NumberFormatException e)
+            {
+                // Ignore
+            }
+        }
+        if (parallelism <= 1)
+        {
+            return new Executor()
+            {
+                public void execute(Runnable command)
+                {
+                    command.run();
+                }
+            };
+        }
+        else
+        {
+            ThreadPoolExecutor executor = new ThreadPoolExecutor(
+                    parallelism, parallelism,
+                    60, TimeUnit.SECONDS,
+                    new LinkedBlockingQueue<Runnable>(),
+                    new ThreadFactory()
+                    {
+                        final AtomicInteger counter = new AtomicInteger();
+                        @Override
+                        public Thread newThread(Runnable r)
+                        {
+                            return new Thread(r, "FelixResolver-" + counter.incrementAndGet());
+                        }
+                    });
+            executor.allowCoreThreadTimeOut(true);
+            return executor;
+        }
+    }
+
     void start()
     {
         m_registry.registerService(m_felix,
                 new String[] { Resolver.class.getName() },
-                m_resolver,
+                new ResolverImpl(m_logger, 1),
                 null);
     }
 
+    void stop()
+    {
+        if (m_executor instanceof ExecutorService)
+        {
+            ((ExecutorService) m_executor).shutdownNow();
+        }
+    }
+
     synchronized void addRevision(BundleRevision br)
     {
         // Always attempt to remove the revision, since

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java?rev=1763359&r1=1763358&r2=1763359&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java
Wed Oct  5 08:14:06 2016
@@ -54,6 +54,7 @@ public interface FelixConstants extends
     String NATIVE_OS_NAME_ALIAS_PREFIX = "felix.native.osname.alias";
     String NATIVE_PROC_NAME_ALIAS_PREFIX = "felix.native.processor.alias";
     String USE_CACHEDURLS_PROPS = "felix.bundlecodesource.usecachedurls";
+    String RESOLVER_PARALLELISM = "felix.resolver.parallelism";
 
     // Missing OSGi constant for resolution directive.
     String RESOLUTION_DYNAMIC = "dynamic";



Mime
View raw message