UniBA
UniBA

Reputation: 1

Going ahead only if a method is completed

I made a server and a client in Java for Windows and all working fine. I have to do an Android client but I have some difficulty implementing that. Firstly, I don't want to use AsyncTask because it's deprecated, so I found on the Internet that I can use Threads with the Runnable interface. In my activity, I have this code.

if (!Client.getInstance().isConnected()) {
    Client.getInstance().connect();
}

if (Client.getInstance().isConnected()) {
    // Staff to do if the socket is connected
} else {
    ConnectionUtils.serverUnreachable(this);
}     

I created Client class, which is a singleton. In this code, when it's running connect() code, the client does not wait for the method to complete, so it run serverUnreachable method (an AlertDialog that advise you that the connection to the server was not successful (but the server tells me that a client connected).

public void connect() {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                socket = new Socket(ip, port);
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());
                connected = true;
            } catch (IOException e) {
                e.printStackTrace();
                connected = false;
            }
        }
    });
    thread.start();
}

The connection takes place, but in the if statement, the program doesn't wait till the end of Client.getIntsance().connect() method.

Upvotes: 0

Views: 69

Answers (1)

Rick Sanchez
Rick Sanchez

Reputation: 4756

When you call thread.start(), you're starting a new Thread, and executing the Runnable passed to it, but that doesn't mean that the main (UI) thread in which you were before waits there.

It immediatly continues to the next instructions, which in you're case is checking for connected flag, which most of the time is 'too soon', so it will be false before your other thread has set it to true.

The solution on your case is to run that code after your thread finishes. If you need to run it on main thread ( in order to interact with Views ), then you can use Activity.runOnUiThread(...) method.

Here's an example:

 public void connect() {
    Thread  thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                socket = new Socket(ip, port);
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());
                connected = true;
            } catch (IOException e) {
                e.printStackTrace();
                connected = false;
            }
            runAfterConnection();
        }
    });
    thread.start();
}

public void runAfterConnection(){

   runOnUiThread(new Runnable() {
        @Override
        public void run() {
          if(Client.getInstance().isConnected()) {
             // Staff to do if the socket is connected
          } else {
             ConnectionUtils.serverUnreachable(this);
          }
        }
   });
}

Upvotes: 1

Related Questions