Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and Socket streams.  RSS feed

 
Ernesto Pye
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it sure is. Any object (in this case, a Socket) can easily be shared among threads.
 
Ernesto Pye
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!