简体   繁体   中英

Returning a value from thread

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];

                }           
            }      
    }

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 (ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM