Interrupt Java Execution in Matlab Using Ctrl-C

4 views (last 30 days)
Is it possible to interrupt execution of a Java method in Matlab using ctrl-c?
This solution is not ideal as:
1)Presumably it is a non-blocking call. Perhaps a secondary blocking call in Matlab would be a sufficient solution, something like: java_class.method() matlab_waiting_function()
2)When calling the method no one is going to think to call the stop thread method. The methods I have in mind may not always be long and may be nested in other functions. The above example doesn't make sense. Ideally the user would stop the call just like other Matlab calls.
One example I have in mind is rewriting urlread's read functionality. I think I can speed it up significantly. In addition one of the problems with the current behavior is the inability to interrupt retrieval of the data requested from a website. The only way to terminate reading currently is for the read to timeout. A decent answer to this question would allow me to interrupt the method.
Any thoughts?
Thanks, Jim

Answers (2)

v1bri
v1bri on 16 Jul 2017
Hey I know this thread has gone a little stale, but I wanted to post my solution just in case anyone from the future has the same question. It turns out you can call back into Matlab via JMI to poll the keyboard event queue and trigger an exception if a "Ctrl-C" is waiting. I wrote this up in more detail on my blog at https://v1ntage.io/2017/07/01/episode-001-handling-matlab-interrupts-in-java/. Let me know what you think.

Walter Roberson
Walter Roberson on 1 Jun 2013
You can take care of issue #2 by providing a wrapper function that does the thread creation and thread stopping on the user's behalf.
Unless, that is, you are planning on the optimization of creating the thread, making several calls to it as a "work thread", and then stopping the thread when there is no more work to be done. If that is the plan, then depending on the architecture, a MATLAB onCleanup() might possibly be suitable to trigger the stopping. Also, such an architecture would seem to be ripe for using a MPI (message passing interface) in which the sending function could have a timer on waiting for the response, and do the thread management on timeout, instead of using control-C
  1 Comment
Jim Hokanson
Jim Hokanson on 1 Jun 2013
The interruption isn't part of the design, it is only if something goes wrong. I have a few use cases in mind, but I've updated the question with a specific example.

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!