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

95
LINES

< > BotCompany Repo | #1017894 // Step in multiple threads Spike [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (5864L/42K).

1  
!7
2  
3  
sinterface Steppable {
4  
  public bool step(); // return false if done
5  
}
6  
7  
sclass MultiThreadStepper {
8  
  Runnable onDone;
9  
  Lock lock = lock();
10  
  L<Thread> threads = synchroList();
11  
  L<Steppable> steppables = synchroList();
12  
  L<Steppable> stepping = synchroList();
13  
  int coresToUse = coresToUse();
14  
  volatile bool cancelled;
15  
  
16  
  *() {}
17  
  *(Steppable... steppables) { addAll(this.steppables, steppables); }
18  
  *(Collection<? extends Steppable> steppables) { addAll(this.steppables, steppables); }
19  
  
20  
  void start {
21  
    print("Using " + n2(coresToUse, "core"));
22  
    repeat coresToUse {
23  
      thread "Multi Thread Stepper" {
24  
        final Thread me = currentThread();
25  
        threads.add(me);
26  
        afterwards {
27  
          bool done;
28  
          {
29  
            lock lock;
30  
            threads.remove(me);
31  
            done = empty(threads);
32  
          }
33  
          if (done) callF(onDone);
34  
        }
35  
        //int i = 0;
36  
37  
        while licensed {
38  
          Steppable s;
39  
          {
40  
            lock lock;
41  
            if (done()) ret;
42  
            //s = get(steppables, mod(i++, l(steppables)));
43  
            s = random(steppables);
44  
            if (s == null) continue with sleep(100);
45  
            steppables.remove(s);
46  
            stepping.add(s);
47  
          }
48  
          
49  
          bool done = false;
50  
          try {
51  
            done = !s.step();
52  
          } catch e {
53  
            _handleException(e);
54  
            done = true;
55  
          } finally {
56  
            lock lock;
57  
            stepping.remove(s);
58  
            if (!done) steppables.add(s);
59  
          }
60  
        }
61  
      }
62  
    }
63  
  }
64  
  
65  
  bool done() { ret empty(steppables) || cancelled; }
66  
  
67  
  void stop {
68  
    cancelled = true;
69  
    cancelThreads(threads);
70  
  }
71  
  
72  
  void cleanMeUp {
73  
    stop();
74  
  }
75  
  
76  
  MultiThreadStepper onDone(Runnable r) { onDone = r; this; }
77  
}
78  
79  
static noeq record MySteppable(int n) implements Steppable {
80  
  int count = 0;
81  
  
82  
  public bool step() {
83  
    if (count++ < 10) {
84  
      print("Stepping: " + n);
85  
      sleep(900+random(100));
86  
      true;
87  
    }
88  
    print("Steppable " + n + " done");
89  
    false;
90  
  }
91  
}
92  
93  
p-exp {
94  
  new MultiThreadStepper(map(iota(10), func(int i) { MySteppable(i) })).onDone(rPrint("Done!")).start();
95  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017894
Snippet name: Step in multiple threads Spike [OK]
Eternal ID of this version: #1017894/17
Text MD5: 76aebeae2f5c1c8c25203940e5e7f506
Transpilation MD5: 459d05df6f43172e5b73c568abd6b98e
Author: stefan
Category: javax / execution
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-19 13:38:15
Source code size: 2281 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 519 / 1101
Version history: 16 change(s)
Referenced in: [show references]