简体   繁体   中英

How to terminate a Java process from a different one?

I am making a CSP solver that calculates all the combinatory solutions of a specific problem like this (briefly put):

// Say we are in a Solver class
public void solve() {
  // find solution...
}

// This would be in a Problem class
problem.getSolver().solve();

There is a good chance that the resolution process takes a long time to finish, like more than a couple of minutes. So far, when it has been taking so long I have just stopped the process via console.

But eventually I am going to post my application as a web application in a host that allows Java apps (side-question: I have seen I can do it in Google Cloud and I have been told also about AWS; are they good options?). This means I, or the user, cannot terminate the process anymore if it takes too long.

I want to add the funcionality of having the option of cancelling the resolution process at will.

So I would declare a new method in the Solver class that would terminate the process, which would effectively stop the resolution process:

public void stopResolutionProcess() {
  // kill the process, therefore, stop the resolution process
}

I cannot just call problem.getSolver().stopResolutionProcess() after the resolution process has already started, because the thread is already running and until that process ends, that method call will never be executed.

So how could I do this? How could a client signal the service hosted in the cloud to terminate a running process?

Since you haven't chosen a cloud host yet this question is really difficult to answer. Generally speaking you need a synchronization object. Something like

volatile boolean keepRunning = true;

and your method would do

public void stopResolutionProcess(){
    keepRunning = false;
}

then in your solve you have to regularly check for that variable

public void solve(){
   while(keepRunning){
       // doSomething();
       Thread.sleep(500);
   }
}

Now i use a variable here but that may not be sufficient. In App Engine your application could run in different instances where static variables are not synchronized. You would need a sync object that is accessible to all your solve threads. In App Engine that would be a datastore object that is also cached in the memcache. But you could use Pub/Sub or other mechanisms to propagate the completion of the task. The specifics are tightly coupled to the environment you'll choose to run this on.

Requests for product recommendations are generally off-topic on Stackoverflow. Choose a product for hosting and if you run into trouble return with a more concrete question.

Put the long running process in a different thread. You can stop it from the main thread if necessary. See:

How to stop a thread by another thread?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM