Multiple threads read from the same socket

I am developing an application that displays data from a server. The server is not mine, and it is not very stable. Creating too many connections causes the server to crash.

I have one socket on the server in my main action, but sometimes I want to open auxiliary operations that read data and display them. My problem is that I cannot achieve this with the same socket and must open a new socket for each action. Each action has a stream that reads from the socket and updates the user interface elements with this action as necessary.

To use the same socket in several actions, I tried to close the inputReader activity before starting a new action, but this just makes the application freeze. If I leave it open, the new thread in the new action will never receive any data. It is impossible to kill a stream before starting a new action, because the stream is usually blocked by the read () function.

Is there anyway that I can have a centralized thread that reads and then sends data to all other threads in other actions, so that I don’t need to open new sockets in every action?

I feel that this is a very simple thing that I am asking for, but still I cannot find a solution.

+4
source share
2

:

  • Service,
  • Service , (, ), LocalBroadcastManager
  • BroadcastReceiver Service onReceive()

Services BroadcastReceivers, , . , , .

EDIT, :

Service, stopService(), -, / Service. Service Thread HandlerThread, . / , (LocalBroadcastManager).


, ( ):

class SocketThread implements Runnable
{
    static final String SOCKET_DATA_RECEIVED = "com.your.package.SOCKET_DATA_RECEIVED";
    static final String SOCKET_DATA_IDENTIFIER = "com.your.package.SOCKET_DATA";
    private Context context;

    SocketThread(Context c) {
        context = c.getApplicationContext();
    }

    @Override
    public void run() { // code running in your thread
        // fetch data from socket ...
        Intent intent = new Intent();
        intent.putExtra(SOCKET_DATA_IDENTIFIER, data); // store data in your intent
        // send data to registered receivers
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
        // your code ...
    }
}

, MyActivity1, MyActivity2,... MyActivityN. SocketDataReceiver, SOCKET_DATA_RECEIVED, .

onReceive() intent SOCKET_DATA_IDENTIFIER.

public class MyActivity1 extends Activity
{
    private SocketDataReceiver socketDataReceiver;

    @Override
    protected void onResume() {
        super.onResume();
        socketDataReceiver = new SocketDataReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(
                socketDataReceiver, new IntentFilter(SocketThread.SOCKET_DATA_RECEIVED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(socketDataReceiver);
    }

    private class SocketDataReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            // intent contains your socket data,
            // get data from intent using SocketThread.SOCKET_DATA_IDENTIFIER
        }
    }
}
+1

:

, , .

: , . , . , , :

enum RequestType { DO_THIS, DO_THAT };

interface ServerConnectionService<T> {
   List<T> performRequest(RequestType request);
}

: , " ", , , : " , , ). , , , .

(, ) ; , .

: - . , , , . , , . , : . , . , .

+1

Source: https://habr.com/ru/post/1655944/


All Articles