• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running two threads -- help!

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic