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

35
LINES

< > BotCompany Repo | #1031007 // moveProducerToNewThread

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

Transpiled version (3318L) is out of date.

1  
static <A> Producer<A> moveProducerToNewThread(Producer<A> producer, CloseablesHolder ch, int bufferSize default 65536) {
2  
  if (producer == null) null;
3  
  SimpleCircularBuffer<A> buffer = new(bufferSize);
4  
  new Flag done;
5  
  new Var<Throwable> error;
6  
  ch.add(() -> done.raise());
7  
  startThread(r {
8  
    try {
9  
      A a;
10  
      while ping (!done.isUp() && (a = producer.next()) != null) {
11  
        ifdef moveProducerToNewThread_debug
12  
          print("original producer returned: " + a);
13  
        endifdef
14  
        while (buffer.isFull() && !done.isUp()) sleep(1);
15  
        buffer.add(a);
16  
      }
17  
    } catch e {
18  
      error.set(e);
19  
    } finally {
20  
      done.raise();
21  
    }
22  
  });
23  
  
24  
  ret () -> {
25  
    while true {
26  
      bool isDone = done.isUp();
27  
      A a = buffer.popFirst();
28  
      if (a != null) ret a;
29  
      if (isDone)
30  
        null;
31  
      else
32  
        sleep(1);
33  
    }
34  
  };
35  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031007
Snippet name: moveProducerToNewThread
Eternal ID of this version: #1031007/6
Text MD5: 6a88618f5a0301b324d8026b27d51495
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-19 18:02:52
Source code size: 905 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 137 / 209
Version history: 5 change(s)
Referenced in: [show references]