0

A simple scenario here,

I have a thread (let's call it A) that writes to terminal indefinitely(using System.out) I need to somehow retrieve those information from another Thread (let's call it B). The problem is that A and B cannot communicate in any other way.

So is there a way that B can retrieve those information from terminal?

Note: This is a prototype I'm designing. thread A can be any other process and not necessarily written in Java, it just runs on a terminal indefinately

nafas
  • 5,283
  • 3
  • 29
  • 57
  • 1
    Really threads? Or processes? And why can't they "communicate in any other way"? – Seelenvirtuose Nov 05 '14 at 10:26
  • 3
    why can they not communicate in anyother way? cant you implement something that allows this? – Philipp Sander Nov 05 '14 at 10:26
  • @Seelenvirtuose its a prototype I'm designing. I'm using threads for now. but it shouldn't make difference if Thread A is even written in another program as long as it write to terminal – nafas Nov 05 '14 at 10:31
  • @nafas In fact, it makes a big difference. If two processes are involved, you can either use the operating system to pipe the output of process A to the input of process B, or - if you are creating the other process yourself in the Java program - you can simply access this process' input and output streams. If you have only one program (so, only one process) with several threads, there are much better alternatives for communication between them. – Seelenvirtuose Nov 05 '14 at 10:38
  • @Seelenvirtuose I understand what you mean mate, I'm aware of alternatives that can be used if using different threads(e.g. shared resource). however my question is not of those alternatives. just a simple java program that reads stuff that are printed in terminal regardless of how those things are printed. – nafas Nov 05 '14 at 10:42
  • There's `System.setOut`, and `PipedInputStream` and `PipedOutputStream`. (I really doubt this is the best way to go, but I'm not going to argue) – user253751 Nov 05 '14 at 10:50

1 Answers1

0

You could just create a class that can hold all the required informations for the threads. Or use a File and just place everything in there (like a "simulated console"), but that's rather ugly than anything else if you ask me..

BluesSolo
  • 608
  • 9
  • 28
  • the idea is thread A just writes on terminal, I cannot be changed at all. – nafas Nov 05 '14 at 10:33
  • 2
    Why you wanna make your threads "communicate" like this at all? The console should be used to communicate with the user and not with another thread, if I'm not mistaken. – BluesSolo Nov 05 '14 at 10:36
  • I probably have something in mind that I've asked the question mate. – nafas Nov 05 '14 at 10:43
  • Weird, but if you say so..maybe [that](http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console) can help you – BluesSolo Nov 05 '14 at 10:50