Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

28
LINES

< > BotCompany Repo | #1029091 // parallelFilter

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2805L/18K).

// filters in parallel, returns elements in correct order
static <A> L<A> parallelFilter(Iterable<A> it, IPred<A> f) ctex {
  if (it == null) null;
  new L<Pair<A, Int>> out;
    
  int poolSize = coresToUse_fixed(), queueSize = 500;
  if (poolSize <= 1) ret filter(it, f);
  NotifyingBlockingThreadPoolExecutor e = new(poolSize, queueSize, 15, TimeUnit.SECONDS);

  try {
    int i = 0;
    for (A o : it) {
      ++i;
      int _i = i;
      e.execute(r { pcall {
        if (f.get(o)) synchronized(out) {
          out.add(pair(o, _i));
        }
      }});
    }
    e.shutdown();
    e.awaitTermination(1, TimeUnit.DAYS);
  } finally {
    e.shutdown();
  }

  ret firstOfPairs(sortBySecondOfPairs_inPlace(out));
}

Author comment

Began life as a copy of #1015446

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029091
Snippet name: parallelFilter
Eternal ID of this version: #1029091/6
Text MD5: ddd33b2825ea9e26931a1cb06692f3d5
Transpilation MD5: e160cc85bce2c3ec52529e336ea529a6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-19 03:16:16
Source code size: 746 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 225 / 306
Version history: 5 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)