Multithreaded return value

I'm working on a Java Project using Sockets. Since the Socket Methods are Blocking Methods, I'm forced to make a multithreaded application.

As of now, I am able to read the incoming bytes from an other application. I write them into a StringBuilder. My problem is how to return this string back to my main thread. I was thinking of something like an Event, which I can call as soon as the Socket-Thread has finished reading.

I am happy for any ideas.

public class SocketRunnable implements Runnable {

private final Socket _socket;


public SocketRunnable(Socket socket){
    this._socket = socket;
}

@Override
public void run() {
    StringBuilder sb = new StringBuilder();
    try {
        BufferedReader bufferedReader = 
                new BufferedReader(
                    new InputStreamReader(
                        this._socket.getInputStream()));


    char[] buffer = new char[100];
    int amountOfBytes = bufferedReader.read(buffer, 0, 100); // blocks until the message is received.
    String message  = new String(buffer, 0, amountOfBytes);
    sb.append(message);

    } catch (IOException ex) {
        Logger.getLogger(SocketRunnable.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally{

        // Return (!)
    }

}

Answers


You can use the Callable interface in conjunction with Executors. You can read about it here:

Callable:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

Executors:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html

Just implement the callable interface with your SocketRunnable class (there is one method - call(), which will return the value from one thread to another thread upon completion).


My problem is how to return this string back to my main thread.

You can't—not due to any technical obstacles, but because it doesn't make conceptual sense. Your main thread has already gone on its way and the context where your return value made sense is lost.

So, either:

  • your main thread must block to receive the result (but what's the point of multithreading then);

  • your main thread is already designed from the ground up in an event-oriented fashion and has an outer event loop which takes events off of a queue and handles them (this is true if you are building a GUI application).

What makes more sense is to make the result available to some other code. It can all happen in the same thread, the one which did the receiving. You can achieve this by simply calling an appropriate method. So, the thing to solve in your design is, how to make an instance which has that method available to the code which is executed in the reading thread.


To display the result into a Swing TextField you can process the result, once you have the string just do:

final String msg=???; // Insert your string here

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        textBox.setText(msg);
    } 
});

The invokeLater call will put you onto the correct thread to modify the contents of the text field.


Write the result String in a blocking queue, and let the main thread poll or take from that queue.


Need Your Help

How to update JavaScript variable using PHP

javascript php

I have a JavaScript file named a pricing.js which contains this content in it:

With or without the end event for request, what's the difference?

javascript node.js

I am new to Node.js. I want to know what's the difference between these two pieces of codes:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.