Error when calling compiled m-file(.jar) in a multi threaded Java app
Asked Answered
S

1

2

I have a simple m-file

function [fRate,Height,Width] = media(filename)  
obj = mmreader(filename);  
fRate = obj.FrameRate;  
Width = obj.Width;  
Height = obj.Height;  
end

Which I have successfully compiled using MATLAB Builder JA into a .jar file.

I have tested the .jar file in a single threaded application and it work with no problem.

The error came when I use it in a multi threaded GUI application. I run the .jar as a thread in one of the class I created and the following error occur.

An unexpected error has been detected by Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9c08b0, pid=5920, tid=4788

Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode windows-x86) Problematic frame:

C [jvm.dll+0x1108b0]

After debugging, I found that the error occur when my thread is calling
media = new Media(); (I name my .jar as Media.jar)

This is my Java code:
// mediaProperty.java

public class mediaProperty implements Runnable {

public void mediaProperty() {

    Matlab_options matlab = new Matlab_options();
    Object[] mediaProp = null;
    java.util.List lstMedia = new ArrayList();
    Media media = null;

    try {

        media = new Media();
        ...

        mediaProp = media.media(3, lstMedia);

        ...
    } catch (Exception p) {
        System.out.println("Exception: " + p.toString());
    } finally {
        MWArray.disposeArray(mediaProp);
        if (media != null) {
            media.dispose();
        }
    }
}

public void run() {
    mediaProperty();
}

}

// GUI.java

private Thread mediap;

if (mediap == null) {
mediap = new Thread(new mediaProperty());
mediap.start();
}

What is wrong? Is it my code?

Sarcoid answered 4/1, 2010 at 4:59 Comment(2)
I've just added my java code. From what I see I have already created a dedicated thread for the .jar but the error alway occur when it try to init the constructor media = new Media();Sarcoid
Adam is right, refer to the following website for detail: mathworks.com/support/solutions/en/data/1-3TIHU3/… So I need to create a 2nd process not thread as the threads I created in my app still share the same process.Sarcoid
U
3

My guess is that MATLAB requires you to access it from exactly one thread. You say it works in a single threaded application, perhaps you need to start a dedicated thread for interacting with MATLAB to get this to work correctly.

See also
Thread safety of Matlab engine API

Unmannerly answered 4/1, 2010 at 5:18 Comment(3)
If this is the case, How do I start a dedicated thread in java?Sarcoid
Well, it depends on the specifics of how you interface with MATLAB. Can you give an example of the Java code you use to call your jar? You probably want to create a thread before you call into your jar at all.Unmannerly
A good book for this is Java Concurrency in Practice. Or look at java.sun.com/javase/6/docs/api/java/util/concurrent/…Unmannerly

© 2022 - 2024 — McMap. All rights reserved.