0

now my main function looks like this:

public static void main(String[] args) {
      ExecutorService executor = Executors.newSingleThreadExecutor();
      executor.submit(() -> {
           new MyClass().start("192.168.1.1", 2000);
      }
      ExecutorService executor1 = Executors.newSingleThreadExecutor();
      executor1.submit(() -> {
           new MyClass().start("192.168.1.1", 2001);
      }
}

In start(...) method I have something like this :

public void start(String first, int second) {
     new Thread(() -> {
     ....
     )}.run();
}

Instead of using an executor, I would like to create a new process for each call of the start(...) function. A process that I will be able to kill at any time. I mean something like this :

public static void main(String[] args) {
      Process process= ...();
      process.submit(() -> {
           new MyClass().start("192.168.1.1", 2000);
      }
      Process process1 = ...();
      process1.submit(() -> {
           new MyClass().start("192.168.1.1", 2001);
      }
}

And i would like to have function to stop a current process :

public void stop(Process process) {
     process.kill();
}

TaskManager after call method main and before call method stop:

TaskManager before call method stop

TaskManager after call method stop :

TaskManager after called method stop

I hope you understand my idea. I read another threads on this page, but there is no exatlcy the same thing what I need. Thanks :)

  • Focus on the 3rd answer in that duplicated question explaining how to use a **ProcessBuilder**. Also understand that you are expected to do research prior posting questions. It is really that you are the first person to ask "java start process" ... – GhostCat Jul 13 '18 at 11:03
  • @GhostCat That's right, I've used this thread before I set up my own. However, I can not translate it into your code. – NewJavaProgrammer Jul 13 '18 at 11:16
  • Sorry, but this isn't a code translation service. Thing is: programming is work. You will have to study how to use the ProcessBuilder class. In essence, you have to start **another** JVM instance, running your main method. You can't have **another** process running inside the same JVM. Either you are using threads, or you have to start *another* JVM in a separate process. – GhostCat Jul 13 '18 at 11:18
  • Or, you step back and rewrite your MyClass so that you can better control from the outside! – GhostCat Jul 13 '18 at 11:18
  • @GhostCat All I need it start and stop processes. When I replaced my executor to process I dont need create threads in start method too. Okay, I understand I must create another JVM instances from my main instance. But how? It's now anwers. – NewJavaProgrammer Jul 13 '18 at 11:52
  • Please listen to what I say: A) the library to start processes is ProcessBuilder. That thing is extensively documented. You have to research how to use it. Then you use that to kick off one or more JVMs running a class B) doing that is pretty inefficient. If you need to tightly control what your threads are doing, then well: write them in ways that allow to do that. For example by enabling to send commands to objects in a different thread, that makes those objects stop doing what they are. – GhostCat Jul 13 '18 at 12:05
  • There is a misconception on your end: this is not a tutor service. You are asking for an advanced topic, and you should not expect that people sit down with you and tell you all the steps in detail. You were told about the *topics* you need to understand, now it is up to you to do the research, and start making experiments. Of course, when you write code, and the code doesn't work, then you are free to ask specific questions to get help. But dont expect us to, well, do *your* work. It is your project. So *you* are the one who has to invest the time for the heavy lifting ... – GhostCat Jul 13 '18 at 12:06
  • I understand that you want a nice little perfect solution that looks almost like what you already have and that is easy to understand. Problem is: that isn't possible. Some things are inherently complicated, and *you wanting* something isn't the same as that being easily achievable. – GhostCat Jul 13 '18 at 12:08
  • @GhostCat, Okay now I understand everything. Thanks for your help. I will get to know with this library. If will have a trouble with my code, i will write in this thread :) Thanks :) – NewJavaProgrammer Jul 13 '18 at 12:19
  • Sounds good. But assuming you really run into code problems: consider to write up a new question then. – GhostCat Jul 13 '18 at 12:22
  • And before I forget about: what is MyClass actually doing? What "thing" do you actually want to run in these processes? – GhostCat Jul 13 '18 at 12:24
  • One object of MyClass send image from camera to server. And I need create process not thread for each camera = each object of MyClass – NewJavaProgrammer Jul 13 '18 at 12:45
  • What operating system is this? And again: what makes you think you need processes? It should be really simple to write some "Uploader" class that can be told to *stop* its work. There is no need to use processes if your only goal is to interrupt that uploading. – GhostCat Jul 13 '18 at 12:49
  • Yes, I do not have to have processes to stop sending. The problem is that the library I use has a memory leak, and there is no contact with the author. Therefore, I am forced to create a process for each webcam and kill it if the memory leak is too large. – NewJavaProgrammer Jul 13 '18 at 12:56

0 Answers0