Home > Software design >  How to get String from another thread?
How to get String from another thread?

Time:02-22

I've got two Threads, Main Thread and other Daemon Thread, which stared by Main Thread. And I've got variable String inside second Thread. I need to get this String from second Thread to Main Thread. Is it possible?

Here is my code:

  public void initialize(URL location, ResourceBundle resources) {
        clientHandler = new ClientHandler(this);
        setAuthenticated(false)
        Network.start(8189);


        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        AbstractMessage msg = Network.readObj();
                        if (msg instanceof AuthName){
                            setAuthenticated(true);
                            createDirectory(msg);
//Need to get this String
                            String path = "client_storage_"   ((AuthName) msg).getName();

                            AuthName name = new AuthName();
                            nickName = name.getName();
                            refreshClientList();
                            refreshServerList();
                            break;
                        }
                    }

CodePudding user response:

I would do this:

public class Idkthename extends Thread{

public String path;

public void run() {
                try {
                    while (true) {
                        AbstractMessage msg = Network.readObj();
                        if (msg instanceof AuthName){
                            setAuthenticated(true);
                            createDirectory(msg);
//Need to get this String
                            path = "client_storage_"   ((AuthName) msg).getName();

                            AuthName name = new AuthName();
                            nickName = name.getName();
                            refreshClientList();
                            refreshServerList();
                            break;
                        }
                    }
}

Or maybe with a getter:

public class Idkthename extends Thread{

private String path;

public String getPath(){
     return path;
}

public void run() {
                try {
                    while (true) {
                        AbstractMessage msg = Network.readObj();
                        if (msg instanceof AuthName){
                            setAuthenticated(true);
                            createDirectory(msg);
//Need to get this String
                            path = "client_storage_"   ((AuthName) msg).getName();

                            AuthName name = new AuthName();
                            nickName = name.getName();
                            refreshClientList();
                            refreshServerList();
                            break;
                        }
                    }
}

So in the main you can do

t.path; or t.getPath();

CodePudding user response:

Another way to pass something from a class is by using an interface that acted as its listener like the following example:

public class Main
{
    public static void main(String[] args)
    {
        Daemon daemon = new Daemon();
        daemon.setListener(new Daemon.DaemonListener() {
            @Override
            public void getMessage(String msg) {
                System.out.println("MESSAGE: "   msg);
            }
        });
        daemon.start();
    }
}

class Daemon implements Runnable
{
    //INTERFACE
    private DaemonListener listener;
    public static abstract interface DaemonListener
    {
        public void getMessage(String str);
    }
    public void setListener(DaemonListener listener)
    {
        this.listener = listener;
    }
    //INTERFACE

    private Thread thread;
    Daemon()
    {
        thread = new Thread(this);
    }
    public void start() { thread.start(); }
    @Override
    public void run()
    {
        System.out.println("Daemon started");
        if(listener != null) listener.getMessage("Hello");
        try {
            for(int i=0; i<10;   i) {
                if(listener != null) {
                    listener.getMessage(String.valueOf(i));
                    Thread.sleep(1000L);
                }
            }
        } catch(Exception e) {}
        System.out.println("Daemon finished");
        if(listener != null) listener.getMessage("World");
    }
}

Output:

Daemon started
MESSAGE: Hello
MESSAGE: 0
MESSAGE: 1
MESSAGE: 2
MESSAGE: 3
MESSAGE: 4
MESSAGE: 5
MESSAGE: 6
MESSAGE: 7
MESSAGE: 8
MESSAGE: 9
Daemon finished
MESSAGE: World
  • Related