Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

message from one object to another

 
robert weninger
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am working on an IRC client have come to the following stumbling block.

This is as much a project in learning how to program in java as it is to accomplish the task of writing an IRC client.

I have 3 classes as follows:
A - Handles connection with IRC server, sends and receives messages from server
B - GUI, displays IRC messages and contains textbox for user input
C - Main class, instantiates classes A and B.

What I want to do is send a string from object A to object B, using using object C as an intermediary. Currently I have this implemented by using a loop to constantly check if the status of the incomming message string in class A (the IRC server communicator) has changes. This results in 100% CPU usage and is probably not the right way to go about this.

My current thinking is that i need to pass a reference of object B into object A as a parameter ?
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds like a plan
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use threads. That would solve your cpu usage problem and could also solve your messaging problem as threads can communicate with each other.

See PipedReader, PipedWriter, PipedInputStream, PipedOutputStream.
[ September 29, 2004: Message edited by: Jeff Bosch ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must advise against threads. Your problem will be much more simply solved with sending references to objects that need references.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by robert weninger:
Hello, I am working on an IRC client have come to the following stumbling block.

This is as much a project in learning how to program in java as it is to accomplish the task of writing an IRC client.

I have 3 classes as follows:
A - Handles connection with IRC server, sends and receives messages from server
B - GUI, displays IRC messages and contains textbox for user input
C - Main class, instantiates classes A and B.

What I want to do is send a string from object A to object B, using using object C as an intermediary. Currently I have this implemented by using a loop to constantly check if the status of the incomming message string in class A (the IRC server communicator) has changes. This results in 100% CPU usage and is probably not the right way to go about this.

My current thinking is that i need to pass a reference of object B into object A as a parameter ?


You should be used Thread in Class A and class A should observer object B every time. if object b change, object a MUST chenge too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic