This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Running two threads -- help!  RSS feed

 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have the following programs using Asterisk-JAVA's ManagerAPI. I want to run 2 threads, along with the handling of the asterisk events. The code follows and the question, below the code.


SeamlessMobility.java (main class)
----------------------------------

------------------
SnmpThread.java (another class)

------------------

To run the SnmpThread thread, I use snmp.start() in the run() method of the main class(SeamlessMobility.java). This also invokes the run() method of the snmp thread. But the events are not handled and they do not get displayed on the java output console, unless I explicitly specify snmp.run() in the registerOnStart() method of the main class (shown in bold).

But since this is not the right way to go about it, can you plz guide me on how do I make the events get displayed on the console, and tell me kindly where I am going wrong?

I am a newbie to Java and thank all much in advance.

Regards,
Aparna
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although there are run() and start() methods all over, I don't see any "extends Thread" or "implements Runnable" anywhere. Can you confirm that, for instance, SnmpThread extends Thread, and that you haven't overridden start()?
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, SnmpThread extends Thread.
And start() has not been overridden in SnmpThread.
It contains the run() method


Can you help me further now??

Thanks
Aparna
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, well, looking at things, I see this:



Here's what I'm thinking: the thread you start on line (1) hasn't done its work yet when this thread gets to line (3) and logs off. If you call snmp.run() at line (2), then of course that code gets to execute on this thread before line (3) does.

So basically the problem is that the logoff needs to wait until the snmp thread is done, and that Thread.sleep() is not doing it! Why not do the logoff on the second thread? Otherwise, this thread needs to wait using snmp.join() before logging off.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!