• 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

Threads

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to run a thread that connects to a UDP socket, listens for incoming data, and processes said data appropriately. The method that does this -- which I've made run() -- works fine; it's well tested.
For some reason, though, depending on how I call it, it behaves oddly. After reading tutorials, I figured I could just create an instance of the object containing that run() method, and then call object.start(). It appeared to work, too; I got the status printout I'd set up to show that the socket was connected, I could send data on the socket (via a different method of that object), and reply data came in OK.
When I tried running the program and then sending it data from another location, without first sending data from it, nothing happened; it apparently never got the data. My first thought was that the socket wasn't properly connected, so I decided to, instead of calling object.start(), called object.run() directly. At this point, data was received and processed appropriately, but I had no control of the proram, as run() obviously had no return method.
The source files in question can be found at www.schnarff.com/gnutonic/; they are runGnutonic.java (main program file) and connectionManager.java (thread implementation).
Any assistance would be greatly appreciated.
Alex Kirk
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex
right off the bat I can tell you that calling run() directly (instead of through start()) will execute the run method in the same thread as the main program - in other words it doesn't create a separate thread of execution. You would call run then when run is done it will return and the next statement after it will executed.
It would be the same as if you'd called start then called join on the very next line. The rest of you program wouldn't finish until run returns.
If I get a chance I'll take a look at the code later today - although I'm not a socket expert.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wonder how much should I study for the threads part if I aim for the SCJP exam.
Seems like there are not much materials on Mughal Rasmussen while other people keep on telling me that's important =)
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will study it well. I had about 5 - 6 questions on threads when I took the exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic