Note that there are some explanatory texts on larger screens.

plurals
  1. POthread join in multithreaded server
    text
    copied!<p>I am implementing a client-server program with a multithreaded server which has an ArrayList to which various clients send data and they are added to the ArrayList.</p> <p>Client :</p> <pre><code>package p2pclient; import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.Scanner; public class client { public static void main(String[] args) { Socket smtpSocket = null; DataOutputStream os = null; DataInputStream is = null; Scanner s = new Scanner(System.in); try { smtpSocket = new Socket("localhost", 8888); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } if (smtpSocket != null &amp;&amp; os != null &amp;&amp; is != null) { try { while(true) { os.flush(); System.out.println("enter line"); String a = s.nextLine(); os.writeBytes(a + "\n"); System.out.println("wrote bytes"); String responseLine; responseLine = is.readLine(); System.out.println("Server: " + responseLine); if (responseLine.indexOf("Ok") != -1) { System.out.println("breaking"); break; } } os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } System.out.println("out of while"); } } </code></pre> <hr> <p>Server thread :</p> <pre><code>package server; import java.io.*; import java.net.*; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; public class ServerThread { public static void main(String args[]) throws InterruptedException { ServerSocket echoServer = null; Scanner s = new Scanner(System.in); String line; ArrayList&lt;String&gt; add_list = new ArrayList&lt;String&gt;(); Collections.synchronizedList(add_list); int number=0; DataInputStream is; PrintStream os; Socket clientSocket = null; try { echoServer = new ServerSocket(8888); } catch (IOException e) { System.out.println(e); } List&lt;Thread&gt; threads = new ArrayList&lt;Thread&gt;(); while(true) { try { clientSocket = echoServer.accept(); /* start a new thread to handle this client */ number++; Thread t = new Thread(new ClientConn(clientSocket,number,add_list)); t.start(); threads.add(t); }catch (IOException e) { System.err.println("Accept failed."); System.err.println(e); System.exit(1); } for (Thread t2 : threads) { t2.join(); } System.out.println(add_list); } } } </code></pre> <hr> <p>Server runnable process :</p> <pre><code> package server; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.ArrayList; class ClientConn implements Runnable { Socket client; DataInputStream in; DataOutputStream out; int s; ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;(); ClientConn(Socket client,int s, ArrayList&lt;String&gt; al){ this.client = client; this.s = s; this.al = al; try { in = new DataInputStream(client.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { out = new DataOutputStream(client.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("deprecation") @Override public void run() { // TODO Auto-generated method stub String response = ""; try { while (true) { response = ""; response = in.readLine(); out.writeBytes(response+"\n"); if (response.indexOf("Ok") == -1) al.add(response); // System.out.println(response); out.flush(); if (response.indexOf("Ok") != -1) { //System.out.println("breaking"); break; } } } catch (IOException e) { System.err.println(e); } } } </code></pre> <p>I am trying to put all the threads in an ArrayList and join on each of the threads on the main thread. This does not work. Instead, each thread waits on the first thread.</p> <p>How do I make the main thread wait for other threads to end before printing the array list ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload