• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pipe or Stream needed?  RSS feed

 
Oliver Gibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I'm new to all this, so go easy on me
I have a thread (one class) that generates output which I wish to have displayed in a jTextArea component (part of another class). I guess this is a simple producer/consumer type arrangement.
What do I need to link the two so that the thread can "output" to the jTextArea of the other class? A pipe, a stream, a buffer? I can't think straight today, so am hoping to lean on someone's better experience here
Hope you can point me to enlightenment!
Thanks in advance,
gibbsie
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pipes are usually used for communication between processes. Communication between threads within the same process is simpler because the threads share the same memory space. I've had the "producer"directly invoke the "consumer"'s methods (i.e. append()), or I've used some shared resource, like a queue.
 
Oliver Gibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Joe for the notes, I think I'm just having a bad day and a complete mind-blocker! I'll have another go in a bit
Thanks again!
gibbsie
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Oliver!

A stream would be adecuate if the origin and sink of information would be in different java virtual machines.
A pipe is used if both the origin and sink are active object, they have its own thread of execution. The textarea does not qualify.
Solution: give visibility to the two classes. That is, provide the "producer" with a reference to the "consumer". And write a method in the "consumer" to add the message to the textarea. Funnily you do not need either a pipe or a stream, but a thread. However not any thread, because the textarea is likely to be shown, you can only modify its state from within the event-dispatching thread. Read the API for SwingUtilies.invokeLater for doing that.
Also this not a producer comsumer scenario. There should be a necessity to synchronize the rate at which both the producer and the consumer perform for being considered as that. Tipically the producer cannot produce a second item untill is notified by the consumer that it has already consumed the first one. And the consumer cannot consume again untill is notified by the producer that a new item is available. Otherwise could consume twice the same item.
 
Oliver Gibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Jose, I'll drink to that
I think yesterday I hit mind-lock (my brain seems to like deadlocks... they happen enough up there!) I've got a clearer head today. I think I just couldn't "see" the light yesterday, ending up totally confusing myself instead!
gibbsie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!