title: How to run DatabaseUtil.precomputedKNNQuery method of LOF class on two different threads
body: <p>I want to reduce runtime of DatabaseUtil.precomputedKNNQuery method by running this method on two different threads and KNNQuery is an interface.</p>
  
  <pre><code>    KNNQuery&lt;O&gt; knnq = DatabaseUtil.precomputedKNNQuery(database, relation, getDistanceFunction(), k);
  </code></pre>
  
  <p>I divided this method of LOF class in two parts like this</p>
  
  <pre><code>       Callable&lt;KNNQuery&gt; task1(Database database, Relation&lt;O&gt; relation){
          DBIDs idss = relation.getDBIDs();
          ArrayDBIDs aids = (ArrayDBIDs) idss;
          aids = aids.slice(0, (aids.size() / 2));
          aids.size();
          ProxyView&lt;O&gt; pv = new ProxyView&lt;&gt;(aids, relation);
          return () -&gt; {
              return DatabaseUtil.precomputedKNNQuery(database, pv, 
          getDistanceFunction(), k);
          };
      }
  
      Callable&lt;KNNQuery&gt; task2(Database database, Relation&lt;O&gt; relation) {
          DBIDs idss = relation.getDBIDs();
          ArrayDBIDs aids = (ArrayDBIDs) idss;
          aids = aids.slice(((aids.size() / 2) - 1), aids.size());
          aids.size();
          ProxyView&lt;O&gt; pv2 = new ProxyView&lt;&gt;(aids, relation);
          return () -&gt; {
              return DatabaseUtil.precomputedKNNQuery(database, pv2, getDistanceFunction(), k);
          };
      }
  </code></pre>
  
  <p>Then i invoked both these tasks on two different threads like this in run() method of LOF class </p>
  
  <pre><code> public OutlierResult run(Database database, Relation&lt;O&gt; relation) {
  StepProgress stepprog = LOG.isVerbose() ? new StepProgress("LOF", 3) : null;
  DBIDs ids = relation.getDBIDs();
  
   LOG.beginStep(stepprog, 1, "Materializing nearest-neighbor sets.");     
   ExecutorService executor = Executors.newFixedThreadPool(2);
   List&lt;Callable&lt;KNNQuery&gt;&gt; callables = Arrays.asList(
              task1(database, relation),
              task2(database, relation));
    for (Future&lt;KNNQuery&gt; future : executor.invokeAll(callables)) {
         KNNQuery&lt;O&gt; knnq = future.get();
    // Compute LRDs
    // compute LOF_SCORE of each db object
    // Build result representation
      }
  }
  </code></pre>
  
  <p>But i am getting exception which is saying something like this because forEach is providing only output of first future in knnq variable but not the combined output of both future's. Please help me how can i get rid of this exception with example thanks?</p>
  
  <pre><code>de.lmu.ifi.dbs.elki.datasource.FileBasedDatabaseConnection.load: 505 ms
  LOF #1/3: Materializing nearest-neighbor sets.
  de.lmu.ifi.dbs.elki.index.preprocessed.knn.MaterializeKNNPreprocessor.k: 4
  de.lmu.ifi.dbs.elki.index.preprocessed.knn.MaterializeKNNPreprocessor.k: 4
  Materializing k nearest neighbors (k=4): 21751 [100%]  de.lmu.ifi.dbs.elki.index.preprocessed.knn.MaterializeKNNPreprocessor.precomputation-time: 21470 ms
  Materializing k nearest neighbors (k=4): 21750 [100%] 
  de.lmu.ifi.dbs.elki.index.preprocessed.knn.MaterializeKNNPreprocessor.precomputation-time: 22355 ms
  LOF #2/3: Computing Local Reachability Densities (LRD).
  Task failed
  de.lmu.ifi.dbs.elki.database.datastore.ObjectNotFoundException: Object 
  21751 was not found in the database.
  at de.lmu.ifi.dbs.elki.database.datastore.memory.ArrayStore.get(ArrayStore.java:69)
  at de.lmu.ifi.dbs.elki.index.preprocessed.knn.AbstractMaterializeKNNPreprocessor.get(AbstractMaterializeKNNPreprocessor.java:118)
  at de.lmu.ifi.dbs.elki.database.query.knn.PreprocessorKNNQuery.getKNNForDBID(PreprocessorKNNQuery.java:84)
  at de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LOF.computeLRD(LOF.java:292)
  at de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LOF.computeLRDs(LOF.java:277)
  at de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LOF.run(LOF.java:244)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at de.lmu.ifi.dbs.elki.algorithm.AbstractAlgorithm.run(AbstractAlgorithm.java:89)
  at de.lmu.ifi.dbs.elki.workflow.AlgorithmStep.runAlgorithms(AlgorithmStep.java:100)
  at de.lmu.ifi.dbs.elki.KDDTask.run(KDDTask.java:109)
  at de.lmu.ifi.dbs.elki.application.KDDCLIApplication.run(KDDCLIApplication.java:58)
  at [...]
  </code></pre>