开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Re: Connect to TWS in main not in Thread (Java)


btw12342001
 

It's just an example so don't expect it to do something else. Your problem isn't really with the threads but the run method (called with .start). That method connects and requests data. If you want more data with the same connection you will have to write it yourself somehow.

You could simply add more lines like

Contract contract = createContract.....
eClientSocket.reqMktData....

but then you'd have to redo the way it handles returned data.

--- In TWSAPI@..., twsapi <no_reply@...> wrote:

Hi board,

I have a question regarding the thread programming in Java with the IB API. In the example application (Example 1) you see that first the main class is called and within that class a new thread is called e.g. new Example1(args[0]).start();
If I move the connect call to TWS e.g. connectToTWS(); into the main class the thread will not see/be able to access the open connection to TWS.

My question is now, why is the thread not able to use the opened connection within main but need its own connection call? And can I actually use just one connection call in main to be used by multiple threads?

What I want to achieve is that the main class opens the initial call to TWS and holds this open while different threads can use this open connection. As every call of connectToTWS(); requires me to allow access in the TWS client I only want to authorize my program once not every time a thread actually opens the connection. Not much of automation when I need to authorize it manually every time :)

Thanks so much for your help!

Regards

Join [email protected] to automatically receive all group messages.