Q:

How do I thread with sockets so that the program doesn't stop

It seems that when I use the accept method from the Socket class the whole program freezes up until data comes through. I've passed the socket to a thread and it doesn't freeze but I need the thread to return data back which I don't think it can do.
Code For getting useername
public boolean checkUsername() {
    NetworkIO n = new NetworkIO();
    // Grabs username from edittext field
    username = usernameEditText.getText().toString();
    System.out.println(usernameEditText.getText().toString());
    // queries databse for username
    try {
        resultFromServer = n.query("username",
                "select username FROM user_info  WHERE MATCH (username)  AGAINST ('"
                        + username + "' IN NATURAL LANGUAGE MODE);");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Code for NetworkIO class
public class NetworkIO extends Activity {
    Socket networkSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

public String query(String request, String fieldRequested)
        throws IOException {

    // Tries to get connection
    try {

        networkSocket = new Socket("192.168.1.8", 9009);

        out = new PrintWriter(networkSocket.getOutputStream(), true);

        in = new BufferedReader(new InputStreamReader(
                networkSocket.getInputStream()));
        System.out.println("Hi from in made");
    } catch (UnknownHostException e) {
        System.err.println("Unknown host");
        System.exit(-1);

    } catch (IOException e) {
        System.err.println("IOException");
        System.exit(-1);
    }

    //Sends Request
    out.println(request);
    out.println(fieldRequested);
    String resultFromServer = "";

    //Waits for response and if its null exit
    while ((in.readLine()) != null) {
        resultFromServer += in.readLine();

    }
    System.out.println(resultFromServer);

    //Close the connections
    out.close();
    in.close();
    networkSocket.close();

    return resultFromServer;
}
}

Any criticism of my code will be much appreciated :)

A:

The usual pattern is to run the accept loop inside a thread, and have a separate thread pool of handlers that you dispatch requests to.
When you get a new socket from accept, hand the socket over to a worker thread to process. That way you don't ever need to pass anything back to your main thread.
Example is straight out of the ExecutorService javadocs: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

