Why should newStatefulKnowledgeSession, which merely *creates* a StatefulKnowledgeSession object from a KnowledgeBase, be able to run in parallel with calls to the same method? It's a lightweight operation, not taking long, and the KnowledgeBase must be protected for a short time to avoid confusion in its internal data structures.

-W


2011/10/11 Juan Carlos Fernández <juancarlos.fernandez@safelayer.com>
Hi all,

Try to execute many threads with the next source code (simple simple code).
You will see that only one CPU-thread is used in a mutithreaded CPU system.
It seems to be a system lock inside newStatefulKnowledgeSession().

This is a big disappoint because we can't use StatefulKnowledgeSession in
multithreaded environment. All is executing sequentially because of this
problem. Yes, we can asure newStatefulKnowledgeSession() is multithread (no
error is returned) but we can't asure newStatefulKnowledgeSession() executes
in parallel.

Do you know any problem about this?

Thank's in advance

PROBLEMATIC SOURCE CODE:

public class KnowledgeSessionThread extends Thread {
       private static final int ITERATIONS = 1000000;
       private KnowledgeBase kbase;

   public KnowledgeSessionThread() throws Exception {
               kbase = readKnowledgeBase();
   }

   public void run() {
               try {
                       for( int i = 0; i < ITERATIONS; i++ ) {
                               StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
                               session.dispose();
                       }
                       System.out.println("Thread finished");
               } catch( Exception e ) {
                       e.printStackTrace();
               }
   }

   private KnowledgeBase readKnowledgeBase() throws Exception {
       KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
       kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"),
ResourceType.DRL);
       kbuilder.add(ResourceFactory.newClassPathResource("Sample.bpmn"),
ResourceType.BPMN2);
       KnowledgeBuilderErrors errors = kbuilder.getErrors();
       if (errors.size() > 0) {
           for (KnowledgeBuilderError error: errors) {
               System.err.println(error);
           }
           throw new IllegalArgumentException("Could not parse
knowledge.");
       }
       KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
       kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
       return kbase;
   }
}



--
View this message in context: http://drools.46999.n3.nabble.com/StatefulKnowledgeSession-construction-executes-sequentially-no-multithread-tp3412107p3412107.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users