The application sends messages to the server, implemented in a separate mThr stream, but I still have to constantly, from the very launch of the application, read information from the server that sends it (every 2 seconds), tried to put this operation into a separate mThr2 stream, but apparently this is done not this way. Tell me how this can be implemented?

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ public static String SERVERIP; public static int SERVERPORT; String message; public String serverMessage; public int i = 0; public Socket client = null; public DataOutputStream dataOutputStream = null; public DataInputStream dataInputStream = null; private DataAdapter mAdapter; public ArrayList<String> move = new ArrayList<String>(); public ArrayList<String> clientList = new ArrayList<String>(); int[][] pm = { {0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0}, {1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0} }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button send = (Button) findViewById(R.id.button); final Button clear = (Button) findViewById(R.id.button2); final Button clientC = (Button) findViewById(R.id.button3); final EditText ips = (EditText) findViewById(R.id.ipstr); final EditText thread = (EditText) findViewById(R.id.editText); final EditText ports = (EditText) findViewById(R.id.portstr); final TextView serverKuka = (TextView) findViewById(R.id.textView2); mThr2.start(); clientC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GraphOfActions g = new GraphOfActions(); move = g.FinalCommandList(clientList); } }); clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { move.clear(); clientList.clear(); i = 0; } }); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SERVERIP = ips.getText().toString(); SERVERPORT = Integer.parseInt(ports.getText().toString()); move.add("LUA_Base(0,0,0.0)^^^"); move.add("end"); String result = ""; for (int j = 0; j < move.size(); j++) { result += move.get(j) + " "; //try { // mThr.start(); // message = move.get(j); //Thread.sleep(Integer.parseInt(thread.getText().toString()) * 1000); // } catch (InterruptedException e) { //} } mThr.start(); message = result; mThr.interrupt(); } }); final GridView g = (GridView) findViewById(R.id.gridView); mAdapter = new DataAdapter(getApplicationContext(), android.R.layout.simple_list_item_1); g.setAdapter(mAdapter); g.setOnItemSelectedListener(this); g.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub i++; clientList.add(mAdapter.getItem(position)); } }); } Thread mThr2 = new Thread(new Runnable() { @Override public void run() { try { InetAddress serverAddr = InetAddress.getByName(SERVERIP); client = new Socket(serverAddr, SERVERPORT); dataInputStream = new DataInputStream(client.getInputStream()); serverMessage = dataInputStream.readUTF(); } catch (IOException e) { } } }); Thread mThr = new Thread(new Runnable() { @Override public void run() { try { dataOutputStream = new DataOutputStream(client.getOutputStream()); //dataOutputStream.writeBytes(message); dataOutputStream.writeUTF(message); serverMessage = dataInputStream.readUTF(); dataOutputStream.flush(); } catch (IOException e) { } } }); @Override public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { //mSelectText.setText("Выбранный элемент: " + mAdapter.getItem(position)); } @Override public void onNothingSelected(AdapterView<?> parent) { //mSelectText.setText("Выбранный элемент: ничего"); } } 

    0