This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
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
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Dave
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
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 =)
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I will study it well. I had about 5 - 6 questions on threads when I took the exam.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads
 
Similar Threads
Doubt in Sockets!!
TCP Socket Creation Failure
Thread's run() not running
Thread Not Starting
start(),run()