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

59
LINES

< > BotCompany Repo | #1002558 - Process Manager Bot (LIVE)

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

Libraryless. Click here for Pure Java version (1183L/8K/26K).

!752

static class Process {
  S name;
  int id;
  Thread thread;
  Runnable code;
  volatile long started, ended;
  volatile O result; // ?
  volatile Throwable error;
}

static new L<Process> processes;
static int idCounter;

synchronized answer {
  if "list processes" exceptionToUser {
    new L l;
    for (Process p : processes)
      // drop the problem fields
      l.add(dropKeys(litlist("thread", "code"), objectToMap(p)));
    ret slackSnippet(structureLines(l));
  }
  
  if "test process" {
    new Process p;
    p.name = "Test process (sleeps for 10)";
    p.code = runnable {
      sleepSeconds(10);
    };
    ret addProcess(p);
  }
}

static S addProcess(final Process p) {
  p.thread = new Thread() {
    public void run() {
      p.started = now();
      try {
        p.code.run();
      } catch (Throwable e) {
        p.error = e;
      }
      p.ended = now();
    }
  };
  p.id = ++idCounter;
  processes.add(p);
  p.thread.start();
  ret "Added & started, process id: " + p.id;
}

static synchronized Process start(S name, Runnable code) {
  new Process p;
  p.name = name;
  p.code = code;
  addProcess(p);
  ret p;
}

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1002558
Snippet name: Process Manager Bot (LIVE)
Eternal ID of this version: #1002558/1
Text MD5: b1d4d5b3fe9cec75f513fd56bf42049b
Transpilation MD5: b126fc271be52f5ed1f69cbb6176dffb
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-02 22:04:30
Source code size: 1202 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 504 / 1029
Referenced in: [show references]

Formerly at http://tinybrain.de/1002558 & http://1002558.tinybrain.de