There is a documentation for Matlab API for Java.
I tried to follow it but it seems not allowed if using multithread in java.
What is the point of allowing Java API or having a multi cpu computer???
Below is my source program that gives the following runtime error:
acquire sem: true evaluating: mytick_helper Exception in thread "Thread-20" java.lang.ExceptionInInitializerError at semwait$mythread.run(semwait.java:36) Caused by: java.lang.IllegalStateException: Initializing MATLAB Engine API from MATLAB is not supported. at com.mathworks.engine.MatlabEngine.<clinit>(MatlabEngine.java:75) … 1 more
import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import com.mathworks.engine.*; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.logging.Level; import java.util.logging.Logger;
public class semwait {
private class mythread extends Thread { Semaphore m_sem; String m_eval; public mythread(Semaphore sem, String eval) { m_sem = sem; m_eval = eval; } @Override public void run() { boolean ret = false; System.out.println("running semwait thread"); try { ret = m_sem.tryAcquire(60, TimeUnit.SECONDS); } catch (InterruptedException ex) { System.out.println("timeout: " + ex.getMessage()); } System.out.println("acquire sem: " + ret); if ((m_eval != null) && (m_eval != "")) { System.out.println("evaluating: " + m_eval); try { // Execute command on shared MATLAB session MatlabEngine eng = MatlabEngine.connectMatlab("darwin"); eng.eval("darwin=5;"); eng.eval("mytick_helper"); eng.close(); } catch (MatlabSyntaxException ex) { Logger.getLogger(semwait.class.getName()).log(Level.SEVERE, null, ex); } catch (CancellationException ex) { Logger.getLogger(semwait.class.getName()).log(Level.SEVERE, null, ex); } catch (ExecutionException ex) { Logger.getLogger(semwait.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(semwait.class.getName()).log(Level.SEVERE, null, ex); } try { } catch (Exception ex) { System.out.println("exception: " + ex.getMessage() + ", str: " + ex.toString()); ex.printStackTrace(); } System.out.println("done semwait thread."); } } } public void hello(String str) { System.out.println("hello world: " + str); } public void hello() { System.out.println("hello world"); } public void wait(Semaphore sem, String eval_str) { new mythread(sem, eval_str).start(); }}
Best Answer