| Author |
Threads and Socket streams.
|
Ernesto Pye
Greenhorn
Joined: Apr 10, 2003
Posts: 13
|
|
I am currently working on an IRC bot, and I wanted to add certain commands that could take a while to be finished (Extra socket utilities, or simply anything that took a *long* time to be processed). I'm pretty new to threads, so I wanted to know if it's possible to create a thread that does this long process, and then outputs some data through the main bot Socket's output stream. Thanks in advance for any help.
|
~void
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24051
|
|
|
Yes, it sure is. Any object (in this case, a Socket) can easily be shared among threads.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Ernesto Pye
Greenhorn
Joined: Apr 10, 2003
Posts: 13
|
|
Do you (or anybody) know where I can find an example of controlling the Socket class from a thread started from the main program class? So that.. The question is, how can I access the 'botSocket' output stream, that is, access it by creating a new 'out' or PrintWriter class instance, or using the one I already created, from the thread 'x' which does some other processing? The purpose of this is to avoid the bot to lag whenever a command that takes a while to process (due to CPU load or simply network latency). Thanks in advance. [ February 05, 2004: Message edited by: Ernesto Pye ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24051
|
|
You just need to store a reference to the Socket someplace where the Thread can get at it. One place is in a member variable in the class that creates it; a better place would be in a member variable in "x", your Thread class. The threads chapter of Sun's Java Tutorial has examples of Threads with member variables, although it could be explained more simply. Still, working your way through the whole Thread tutorial now will save you lots of grief later.
|
 |
Ernesto Pye
Greenhorn
Joined: Apr 10, 2003
Posts: 13
|
|
Er, never mind. The answer was simple. Thanks anyway! EDIT: Wow that was a fast reply. Yes that was exactly what I did.. the answer was in front of me the whole time.. Thanks for the help  [ February 05, 2004: Message edited by: Ernesto Pye ]
|
 |
 |
|
|
subject: Threads and Socket streams.
|
|
|