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

52
LINES

< > BotCompany Repo | #1015446 // parallelMap2

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

Libraryless. Click here for Pure Java version (3179L/19K).

sbool parallelMap2_debug;

// Note: an executor passed in is not shut down 

static <A, B> L<B> parallelMap2(ThreadPoolExecutor executor default null, Collection<A> l, F1<A, B> f) {
  if (l(l) <= 1)
    ret map(f, l);
  ret parallelMap2(executor, iterator(l), l(l), f);
}

static <A, B> L<B> parallelMap2(ThreadPoolExecutor executor default null, Iterator<A> it, final int count, final F1<A, B> f) ctex {
  if (executor == null && coresToUse_fixed() == 1)
    ret map(f, iterable(it));
    
  ThreadPoolExecutor e = executor == null ? defaultThreadPoolExecutor() : executor;
    
  L<Future<B>> futures = emptyList(count);
  new Var<Throwable> error;
  if (parallelMap2_debug)
    print("parallelMap2: " + count + " elements, " + e.getMaximumPoolSize() + " threads");
    
  long time = sysNow();
  try {
    int i = 0;
    for (final A o : iterable(it)) {
      ++i;
      final int _i = i;
      futures.add(e.submit(() -> {
        try {
          ret callF(f, o);
        } catch e2 {
          error.set(e2);
          null;
        }
      }));
    }
    /*if (parallelMap2_debug)
      print("parallelMap2: scheduling done after " + (sysNow()-time));*/
    L<B> out = getFutures(futures);
    if (error.has())
      throw rethrow(error!);
    if (parallelMap2_debug)
      print("parallelMap2: main done after " + (sysNow()-time));
    ret out;
  } finally {
    if (executor == null) e.shutdown();
  }
}

static <A, B> L<B> lambdaMapLike parallelMap2(IF1<A, B> f, Cl<A> l) {
  ret parallelMap2(l, if1ToF1(f));
}

Author comment

Began life as a copy of #1011925

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015446
Snippet name: parallelMap2
Eternal ID of this version: #1015446/23
Text MD5: 63049ad9c74bd3d86eca5ea14399f449
Transpilation MD5: 824fa5596d60f016b8a1058247d2a761
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-07 03:07:42
Source code size: 1574 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 487 / 617
Version history: 22 change(s)
Referenced in: [show references]