This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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
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
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 =)