3

In a Java program I need to execute a method of a third-paryt java class. I use Class.forName to get a class and newInstance() to get an object of this class:

Class  class  = Class.forName(className);
AClass object = (AClass) class.newInstance(); 

Then I execute and get a result of the desired method with

ResultObject result = object.method();

So far, so good. But the problem appears if I want to limit the execution time of the method (i.e., if the method doesn't stop after a given number of seconds, I would like to kill this execution and ignore the result).

An ideal solution would be to run the method in a separate thread and to stop&destroy this thread if the method doesn't finish in time. But unfortunately a thread CAN NOT BE STOPED (it's stop() method is deprecated) and I can not rely on correct interrupt-behaviour of the third-party method.

Is there any other way to execute a method and to have a full control over its execution (i.e. to be able to kill it any time)? I am sure that this is possible - Netbeans, for example, can execute my class (when I run my application) and it can kill it if I press stop button. But how does Netbeans (or Eclipse or any othe IDE) manage to do this?

Any idea? Thanks!

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
SIzif
  • 31
  • 3
  • 2
    Netbeans runs your application in an entirely separate JVM. – user253751 Nov 18 '14 at 08:15
  • 2
    Look at this: http://stackoverflow.com/questions/2275443/how-to-timeout-a-thread. It says to use ExecutorService and through the Future object how to stops the execution of the thread – Michael Nov 18 '14 at 08:15
  • Do you have control over the called method? – Avi L Nov 18 '14 at 08:17
  • 1
    @MichaelZucchetta that's a good idea but it might not work. The ExectuorService.shutdownNow() explicitly states the following: There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate. – David ten Hove Nov 18 '14 at 08:25
  • @DavidtenHove That's right, the task might still run in the background, but at least the calling method does not wait for its completion any more. If we can assume that the task will not run forever, this solution might be sufficient. – isnot2bad Nov 18 '14 at 08:28
  • @isnot2bad agreed. If that is sufficient it is definitely the best solution. – David ten Hove Nov 18 '14 at 08:31
  • See also this example for a generic timed run implementation from Brian Goetz and Tim Peierls: http://jcip.net.s3-website-us-east-1.amazonaws.com/listings/TimedRun.java – isnot2bad Nov 18 '14 at 08:31
  • A thread that can not be completely stoped is not a good solution for me as I am going to subsequently execute many copies of the method. Several interrupted (but not really stoped) methods would slow down the program drastically. – SIzif Nov 18 '14 at 19:30
  • @immibis: How does Netbeans run a separate JVM and how does it communicate with that JVM? It looks as a perfect solution for my problem to have a separate JVM over which I could have total control (to execute and to kill applications). – SIzif Nov 18 '14 at 19:34

1 Answers1

5

One option would be to run the 3rd party code not just in a seperate thread, but in a seperate process. It's a hassle, but you can kill the process whenever you want to and it's guaranteed to work.

David ten Hove
  • 2,748
  • 18
  • 33
  • 1
    Side note: You can use standard I/O (via streams) to communicate with the subprocess, or - in case you need high sophisticated inter-process-communication - use RMI. – isnot2bad Nov 18 '14 at 08:34
  • Usefull question about it : http://stackoverflow.com/questions/636367/executing-a-java-application-in-a-separate-process – ToYonos Nov 18 '14 at 08:54
  • What exactly does it mean to run a code in a separtate process? To run it with `Runtime.getRuntime().exec("java MyClass");` or is there another way to do this? – SIzif Nov 18 '14 at 20:51