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).

1  
sbool parallelMap2_debug;
2  
3  
// Note: an executor passed in is not shut down 
4  
5  
static <A, B> L<B> parallelMap2(ThreadPoolExecutor executor default null, Collection<A> l, F1<A, B> f) {
6  
  if (l(l) <= 1)
7  
    ret map(f, l);
8  
  ret parallelMap2(executor, iterator(l), l(l), f);
9  
}
10  
11  
static <A, B> L<B> parallelMap2(ThreadPoolExecutor executor default null, Iterator<A> it, final int count, final F1<A, B> f) ctex {
12  
  if (executor == null && coresToUse_fixed() == 1)
13  
    ret map(f, iterable(it));
14  
    
15  
  ThreadPoolExecutor e = executor == null ? defaultThreadPoolExecutor() : executor;
16  
    
17  
  L<Future<B>> futures = emptyList(count);
18  
  new Var<Throwable> error;
19  
  if (parallelMap2_debug)
20  
    print("parallelMap2: " + count + " elements, " + e.getMaximumPoolSize() + " threads");
21  
    
22  
  long time = sysNow();
23  
  try {
24  
    int i = 0;
25  
    for (final A o : iterable(it)) {
26  
      ++i;
27  
      final int _i = i;
28  
      futures.add(e.submit(() -> {
29  
        try {
30  
          ret callF(f, o);
31  
        } catch e2 {
32  
          error.set(e2);
33  
          null;
34  
        }
35  
      }));
36  
    }
37  
    /*if (parallelMap2_debug)
38  
      print("parallelMap2: scheduling done after " + (sysNow()-time));*/
39  
    L<B> out = getFutures(futures);
40  
    if (error.has())
41  
      throw rethrow(error!);
42  
    if (parallelMap2_debug)
43  
      print("parallelMap2: main done after " + (sysNow()-time));
44  
    ret out;
45  
  } finally {
46  
    if (executor == null) e.shutdown();
47  
  }
48  
}
49  
50  
static <A, B> L<B> lambdaMapLike parallelMap2(IF1<A, B> f, Cl<A> l) {
51  
  ret parallelMap2(l, if1ToF1(f));
52  
}

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: 385 / 479
Version history: 22 change(s)
Referenced in: [show references]