my dog learned polymorphism
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Threads" Watch "Threads" New topic


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 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; they are (main program file) and (thread implementation).
Any assistance would be greatly appreciated.
Alex Kirk
Dave Vick
Ranch Hand

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

Sun Certified Programmer for the Java� 2 Platform

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.

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: Threads
jQuery in Action, 3rd edition