This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Urgent Newbie Sockets and Threads - HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Urgent Newbie Sockets and Threads - HELP" Watch "Urgent Newbie Sockets and Threads - HELP" New topic
Author

Urgent Newbie Sockets and Threads - HELP

Protorium Exodious
Greenhorn

Joined: Dec 10, 2001
Posts: 6
Hi, Im a complete newbie at Java although experienced in other stuff
Im trying to create a multi-threaded chat program, but Im having mega-problemos with Threads
Can someone add a few pointers in my code here cos I can get the application, send and receive messages but just cannot seem to do the Thread bit (
btw, this is code Ive been playing with, waits just b4 going into thread
Doh, cant post code (damn router, dont know y)
code is displayed here
http://www.malcolm.kadmon.co.uk/helpme.txt
[This message has been edited by Protorium Exodious (edited December 10, 2001).]
ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
interesting name
hope this helps you to find out the one kind of ordinary : www.javaranch.com/name.jsp
Joel Peach
Greenhorn

Joined: Oct 12, 2001
Posts: 19
Okay, the promotion doesn't officially start for another hour, but let's get a head start.
Take a look http://www.traceris.com/javaranch/ThreadManager.java
This gives a very basic framework for what it appears you are trying to do. The tricky thing to learn about threading is how the code actually gets run. When you create a thread, you either extend the Thread class, or implement Runnable. This is usually a design call in many cases as extending Thread doesn't not allow you to extend any other classes (as you do in your code), whereas Runnable gives you some flexibility.
If you've implemented Runnable, some code somewhere has to create a new instance of the Thread by calling:
Thread t = new Thread(new MyCustomThreadClass());
Once you have a handle to a thread object, you need only call its start() method. What that does is tell the thread scheduler that your thread is ready to be run. When the thread scheduler is ready to run your thread it calls the run() method that you implemented in your Thread class. The run method is called once and only once, then the thread dies, so if you want to keep it alive you need to do so in your run() implementation (conditional loops work well for this, e.g. while(true){ }). Instead of hard-coding "true" you could make the flag variable so that the logic of the run() method could make it stop. When a thread has died, attemps to call start on it again will generate RuntimeExceptions. You can however, call any other method on the thread object as it is still a living object until it is garbage collected.
That's the whirlwind tour of how threading works. As far as it applies in creating socket servers, a common technique is what I've done in my example. There is a main managing class which opens up a server socket and listens for incoming connections via the accept() method. When a new connection comes in, it creates an instance of a special thread passing the connection Socket as an argument to the constructor. It then schedules the thread to run via the start() method. After that happens, the manager class goes about its business and continues listening for connections. Meanwhile, the thread schedule shares time for the threads we've created and they process the socket as dictated by the thread's run() method.
That should be a good place to start looking at ideas to improve the function of your code. You'll also notice that my class employed an inner class as the Thread implementation. This technique actually makes it possible for the thread class to access member variables and methods of the enclosing ThreadManager class. This would be useful if the thread needed to do something like pass control codes (enter chat/exit chat) or post something like messages into a main queue.
The possibilities are limited only by your imagination.
Best of luck!
-Joel
jpeach@traceris.com
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
Originally posted by Joel Peach:
Okay, the promotion doesn't officially start for another hour, but let's get a head start.
Take a look http://www.traceris.com/javaranch/ThreadManager.java
This gives a very basic framework for what it appears you are trying to do. The tricky thing to learn about threading is how the code actually gets run. When you create a thread, you either extend the Thread class, or implement Runnable. This is usually a design call in many cases as extending Thread doesn't not allow you to extend any other classes (as you do in your code), whereas Runnable gives you some flexibility.
If you've implemented Runnable, some code somewhere has to create a new instance of the Thread by calling:
Thread t = new Thread(new MyCustomThreadClass());
Once you have a handle to a thread object, you need only call its start() method. What that does is tell the thread scheduler that your thread is ready to be run. When the thread scheduler is ready to run your thread it calls the run() method that you implemented in your Thread class. The run method is called once and only once, then the thread dies, so if you want to keep it alive you need to do so in your run() implementation (conditional loops work well for this, e.g. while(true){ }). Instead of hard-coding "true" you could make the flag variable so that the logic of the run() method could make it stop. When a thread has died, attemps to call start on it again will generate RuntimeExceptions. You can however, call any other method on the thread object as it is still a living object until it is garbage collected.
That's the whirlwind tour of how threading works. As far as it applies in creating socket servers, a common technique is what I've done in my example. There is a main managing class which opens up a server socket and listens for incoming connections via the accept() method. When a new connection comes in, it creates an instance of a special thread passing the connection Socket as an argument to the constructor. It then schedules the thread to run via the start() method. After that happens, the manager class goes about its business and continues listening for connections. Meanwhile, the thread schedule shares time for the threads we've created and they process the socket as dictated by the thread's run() method.
That should be a good place to start looking at ideas to improve the function of your code. You'll also notice that my class employed an inner class as the Thread implementation. This technique actually makes it possible for the thread class to access member variables and methods of the enclosing ThreadManager class. This would be useful if the thread needed to do something like pass control codes (enter chat/exit chat) or post something like messages into a main queue.
The possibilities are limited only by your imagination.
Best of luck!
-Joel
jpeach@traceris.com

Hi ,
Thank you for your reply, Though I am new I follow the same practice. However I have a small problem which I have posted in the thread http://www.javaranch.com/ubb/Forum8/HTML/000623.html
It would be helpful if you can suggest some ideas.


------------------
Shubhrajit :-)


Shubhrajit
Protorium Exodious
Greenhorn

Joined: Dec 10, 2001
Posts: 6
Thankyou Joel Peach, exactly what i need, I started understanding last night, your information helps me even more
thx
btw, Protorium Exodious is Greek same as my middle name which is as u may guess, Greenhon meaning beginner
[This message has been edited by Protorium Exodious (edited December 11, 2001).]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Urgent Newbie Sockets and Threads - HELP
 
Similar Threads
Thread yield method
i have question in Thread.. plz help me
Thread Synchronization
Deamon thread how to
Threads Fear