Multithreading Matlab with Java 2In this second part of Multithreading Matlab with Java series we will look into how to pass parameters back and forth between Matlab and Java. It is actually relatively simple, and requires a minor change in our code. But before we'll look into that, I'll go over how to compile Java from the Linux command prompt.
Compiling Java (*.java) files and putting them in a JAR fileAgain, I am not a JAVA guru... I am sort of sharing my experience with you guys so if there is a better way to do this, please feel free to write me and I'll share your ideas with everyone. (20080527-Mental note to self, add a contact me section ;)) So say you have a java code in a file called ThreadJob.java and say it is in a folder called test. I am not sure if you have to do this but AFAIK it is considered good practice to locate your java code inside folders with the same name as their package name. In other words the package declaration for ThreadJob.java is test. Then all you need to do to compile (I should really say pseudo-compile, since it is Java, never converted to actual machine code, hence platform free): cd projects/test cd .. javac test/ThreadJob.java
Above commands will create a ThreadJob.class file inside folder test. And you can run this code using (if the code has a main routine of course): java test/ThreadJob And finally to package this file into a JAR file: jar cvf test.jar test/*.class Then you can add this jar file to the Matlab's class path and call it from java. Terrence Parr has an in depth explanation of above procedure in his lecture notes. Please see the Related Links section for his website reference. Transferring Variables between Java and MatlabIn order to achieve this we will need to add a few lines of code to our ThreadJob example. I will add a class wide variable called Result for example. private int Result=0; And I'll add a getter method for this variable as follows: public int getResult() { return Result; }Now we have a variable and a method to read its value. But the we didn't modify our variable. So let's add another line below the catch block: Result=1; What we did is basically this. We have a Result variable that will be set to zero once our class is initiated. Then it will remain as it is until the thread is done, and will be set to 1 once the thread is completed. But how do we read it in Matlab? Well, we can first create a pointer to our current instance. We can do this for our ThreadJob example as follows (do not forget to add the path to Matlab using javaaddpath): >> a=test.ThreadJob(1); Then we read the Result variable. and it will not change until the thread job is completed. >> a.getResult() ans = 0 >> java.lang.Thread(a).start Starting job 1. >> a.getResult() ans = 0
Value of Result should be 1 once the job is finished right? Here is the Matlab output for that: Finished job 1. >> a.getResult() ans = 1
Above example shows us how to read values from Java into Matlab. How can we pass a variable from Java to Matlab though? Well you have already done it. When you call "test.ThreadJob(1);" within Matlab you are passing the id variable of Java the value of 1. Another way of passing values from Matlab to Java is to use setters. So for our example variable a setter can be created as: public void setResult(int inResult) { Result=inResult; }But one should keep in mind the conversion of variable types between Java and Matlab. The second link in the first article of this series has great information about this topic under the links "Passing Data to a Java Method" and "Handling Data Returned from a Java Method". Files:ThreadJob.java Related Links:Terrence Parr's Lecture Notes for Compiling and Running Java Multithreading Matlab with Java |