multithreading - JAVA real time consol-control the threads -


i beginner in programming. still learning threads , more of them. have quite big idea write first program (i mean bigger simple calculator). want sort files, integrate in 1 (many copies of 1 file in different localization - idea of of no importance now).

but want create threads, or else (what advice). mean. when start program, console starts up, , e.g have write "my_programm run" or "my_program stop" or "my_program status" or "my_magic_tricks be_done". mean how can create program working in background in threads with real time string control on it. tried find out on google useful me, didn't find out.

please give me name of libraries or methods, can use. read out, , move forward it.

if dumbass question. sorry disapointing programmer group. nice given of signpost or clue.

a simple way using standard library :

import java.util.scanner; import java.util.concurrent.linkedblockingdeque; import java.util.concurrent.threadpoolexecutor;  import static java.util.concurrent.timeunit.milliseconds;  public class example {     private static final int pool_size = 5;     private static final executorservice workers = new threadpoolexecutor(pool_size, pool_size, 1, milliseconds, new linkedblockingdeque<>());      public static void main(string[] args) {         scanner sc = new scanner(system.in);         while (true) {             system.out.print("> ");             string cmd = sc.nextline();             switch (cmd) {                 case "process":                     workers.submit(newexpensivetask());                     break;                  case "kill":                     system.exit(0);                  default:                     system.err.println("unrecognized command: " + cmd);             }         }     }      private static runnable newexpensivetask() {         return () -> {             try {                 thread.sleep(10000);                 system.out.println("done processing");             } catch (interruptedexception e) {                 throw new runtimeexception(e);             }         };     } } 

this code lets run heavy tasks asynchronously while user terminal remains available , reactive.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -