In the J/Link User Guide there is no example on how to use java.lang.Thread, from inside Mathematica.
I'd like to know if is possible to start a thread from a JavaNew
object which
has a Runnable interface implemented.
If I run this code from a java main class everything works fine. The Thread will run sampling bid/ask from a tcp/ip data stream and every time I need to know the bid/ask data I call the method that get me the value.
t3OpenT job01 = new t3OpenT("192.168.0.78",5333,500000,200);
job01.openConnection();
job01.setRequest("function=subscribe|item=MI.EQCON.1|schema=best_bsiz1;best_bid1;best_ask1;best_asiz1;symbol");
job01.subscribeData();
System.out.println("now start the thread 01");
Thread ntJob01 = new Thread(job01);
ntJob01.setDaemon(true);
ntJob01.setName("GENERALI");
ntJob01.start();
But if I create the objects inside the notebook using JavaNew[....] and then start the thread it will execute but it is interrupted immediately.
Here is the mathematica code I use:
In[33]:= generaliGRT = t3OpenObjectT[{generaliG}]
Out[33]= {{"1", "G", JLink`Objects`vm1`JavaObject27681839799861249}}
(*Now we setup T3OpenObject to subscription:*)
In[34]:= generaliGRT[[1, 3]]@
setRequest[
"function=subscribe|item=MI.EQCON.1|schema=best_bsiz1;best_bid1;best_ask1;\
best_asiz1;symbol"]
(*subscribe the data on GENERALI stock*)
In[35]:= generaliGRT[[1, 3]]@subscribeData[];
generaliGRT[[1, 3]]@getResponse[]
Out[36]= "outcome=OK|item=MI.EQCON.1"
(*the response is OK so now we are redy to start the thread *)
In[37]:= th = JavaNew["java.lang.Thread", generaliGRT[[1, 3]]]
Out[37]= JLink`Objects`vm1`JavaObject8825827030466561
th@setDaemon[True]
In[39]:= th@setName["GENERALI"]
In[40]:= th@getName[]
Out[40]= "GENERALI"
In[41]:= th@start[]
In[42]:= th@getId[]
Out[42]= 14
(*the thread is immediately terminated *)
In[43]:= th@getState[]@toString[]
Out[43]= "TERMINATED"
(*but I can read the pusched stock date*)
In[45]:= generaliGRT[[1, 3]]@getInfo[]
Out[45]= {"MI.EQCON.1", "18387", "17.19", "17.21", "14300", "G"}
and here is the run() method of T3OpenT class:
@Override
public void run(){
thExtSignal = false;
refThread = Thread.currentThread();
try{
while (true){
try{
//check if thread must be terminated
if(thExtSignal == true){
Thread.currentThread().interrupt();
}
if(Thread.interrupted()){
throw new InterruptedException();
}
//read data from buffer
this.pushedData = this.refBufReader.readLine();
//get the time stamp
dateTimeSample = dateFormat.format(cal.getTime()).toString();
//split the response end move data in a string vector
String responseBidAsk[] = pushedData.split("\\|", 6);
//update only elements tha are changed
for(int i=0 ; i<=responseBidAsk.length-1;i++){
if(responseBidAsk[i].length()>0){
info[i]=responseBidAsk[i];
}else{
info[i]=info[i];
}
}
}catch (InterruptedException e) {
//if the thread is interrupted then unsubscribe
this.unsubscribe();
// We've been interrupted: no more messages.
return;
}
}
}
catch(SocketTimeoutException e){
//if timeout unsubscribe
response = "timeOut";
unsubscribe();
}
catch (IOException e) {
pushedData = "error";
}
}