0

First of all, yes I looked up this question on google and I did not find any answer to it. There are only answers, where the thread is FINISHED and than the value is returned. What I want, is to return an "infinite" amount of values.

Just to make it more clear for you: My thread is reading messages from a socket and never really finishes. So whenever a new message comes in, I want another class to get this message. How would I do that?

public void run(){
            while(ircMessage != null){              
            ircMessage = in.readLine();
            System.out.println(ircMessage);          
                if (ircMessage.contains("PRIVMSG")){                   
                    String[] ViewerNameRawRaw;
                    ViewerNameRawRaw = ircMessage.split("@");

                    String ViewerNameRaw = ViewerNameRawRaw[2];
                    String[] ViewerNameR = ViewerNameRaw.split(".tmi.twitch.tv");
                    viewerName = ViewerNameR[0];

                    String[] ViewerMessageRawRawRaw = ircMessage.split("PRIVMSG");
                    String ViewerMessageRawRaw = ViewerMessageRawRawRaw[1];
                    String ViewerMessageRaw[] = ViewerMessageRawRaw.split(":", 2);
                    viewerMessage = ViewerMessageRaw[1];

                }           
            }      
    }
TomiG
  • 17
  • 5
  • Well, you should have tried to understand those answer, `run()` doesn't return anything, so this generaly call a method (using event or something else), this can be done when you want. – AxelH Jan 31 '17 at 11:37
  • 1
    You can use a queue where the thread will put the messages. Users would take the messages from the queue. see http://stackoverflow.com/questions/2332537/producer-consumer-threads-using-a-queue – Ashwinee K Jha Jan 31 '17 at 11:39
  • @AxelH I know that it doesnt return anything. Thats my question. "How do I return values from this thread" ? – TomiG Jan 31 '17 at 11:40
  • @TomiG, well, you said yourself _where the thread is FINISHED and than the value is returned_ so you seems to not understand that. There is no return, yes at the end of the `run()` a call was probably done to send the value, but this can be done anywhere in the code and as many time as you want depending on the implementation behind this method. – AxelH Jan 31 '17 at 11:42
  • @AshwineeKJha thank you for your answer, I'll try it out – TomiG Jan 31 '17 at 11:45

2 Answers2

0

What you are describing is a typical scenario of asynchronous communication. Usually solution could be implemented with Queue. Your Thread is a producer. Each time your thread reads a message from socket it builds its result and sends it into a queue. Any Entity that is interested to receive the result should be listening to the Queue (i.e. be a consumer). Read more about queues as you can send your message so that only one consumer will get it or (publishing) means that all registered consumers may get it. Queue implementation could be a comercialy available products such as Rabbit MQ for example or as simple as Java provided classes that can work as in memory queues. (See Queue interface and its various implementations).

Another way to go about it is communication over web (HTTP). Your thread reads a message from a socket, builds a result and sends it over http using let's say a REST protocol to a consumer that exposes a rest API that your thread can call to.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Another approach is having a listener, an interface to be called when an event occurs (search for Observer Pattern) – gusto2 Jan 31 '17 at 17:18
  • But the listener will operate in the same thread as it will be called from within this thread, and that is not what OP wanted – Michael Gantman Jan 31 '17 at 19:01
0

Why not have a status variable in your thread class? You can then update this during execution and before exiting. Once the thread has completed, you can still query the status.

public static void main(String[] args) throws InterruptedException {

    threading th = new threading();

    System.out.println("before run Status:" + th.getStatus());
    th.start();
    Thread.sleep(500);
    System.out.println("running Status:" + th.getStatus());
    while(th.isAlive()) {}
    System.out.println("after run Status:" + th.getStatus());
}

Extend thread to be:

public class threading extends Thread {

    private int status = -1; //not started

    private void setStatus(int status){
        this.status = status;
    }

    public void run(){
        setStatus(1);//running

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }

        setStatus(0); //exit clean
    }

    public int getStatus(){
        return this.status;
    }
}

And get an output of:

before run Status:-1
running Status:1
after run Status:0
T. Daves
  • 40
  • 1
  • 9