Hi programmer all over the world In order to understand what's the difference between Concurrency vs Parallelism, I was given this problem to solve but I fall off in this problem that I couldn't solve and it took me a lot of time , so I came here and I home someone could help me . I have a problemm here I have built a program that has 4 classes , 2 of them are "Client" (Agsrq to send generates squares numbers and AgFibo so send sequence of number of fibonacci) used to send number to the server (agclassserver recieve data from Agsqr and Agfibo to order and display them in form of list ) , and I add other class to make server accepts many clients (Multithreading) in this case ,Agsqr and AgFibo are taking the role of client.
So here is my problem , when I execute the classes (agclassserver "server" , Agsqr & AgFibo "Clients") the result is giving me only the fist client lets say "Agsqr" and when I execute the AgFibo it doesn't executes but it gives me another execution of Agsqr "so I have the result of two Agsqr "
Here is all the class I vae used in this program
Agclassserver
package smatp2; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Collections; import java.util.concurrent.*; public class agclassserver { public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub ArrayList<Integer> array = new ArrayList<Integer>(); ArrayList<ClassesHandler> classes_handler = new ArrayList<ClassesHandler>(); ExecutorService service= Executors.newFixedThreadPool(10); boolean var=true; int message; try { while(true) { ServerSocket server = new ServerSocket(9090); System.out.println("I waiting for the client "); Socket socket = server.accept();// waiting for client to connect with server ClassesHandler client = new ClassesHandler(socket); classes_handler.add(client); service.execute(client); } }catch (Exception e) { } } } Agsqr
package smatp2; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; public class Agsqr { public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub try { Socket socket = new Socket ("localhost", 9090); int n=0; OutputStream output = socket.getOutputStream(); DataOutputStream stream = new DataOutputStream(output); while(true) { int result = n*n; stream.writeInt(result); n++; Thread.sleep(1000); } }catch(Exception e) { } } } AgFibo
package smatp2; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class AgFibo extends Thread { public synchronized void run() { } public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException { // TODO Auto-generated method stub try { int number ; int previousNumber = 0; int nextNumber = 1; boolean variable=false ; Socket socket = new Socket ("localhost", 9090); int n=0; OutputStream output = socket.getOutputStream(); DataOutputStream stream = new DataOutputStream(output); while(!variable){ stream.writeInt(previousNumber); int sum = previousNumber + nextNumber; previousNumber = nextNumber; nextNumber = sum; Thread.sleep(1000); } }catch(Exception e) { } } } ClassesHandler
package smatp2; import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.Collections; public class ClassesHandler implements Runnable { private Socket socket; private int message; ArrayList<Integer> array = new ArrayList<Integer>(); private DataInputStream dataInputStream; public ClassesHandler(Socket socket) throws IOException{ this.socket = socket; dataInputStream = new DataInputStream(socket.getInputStream()); } @Override public void run() { // TODO Auto-generated method stub try { while(true) { message = dataInputStream.readInt(); array.add(message);// waiting for client to connect with server Collections.sort(array); System.out.println(message); System.out.println(array); //Thread.sleep(500); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } agclassserver /* Sorry I forgot to post the classserver Class */
package smatp2; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Collections; import java.util.concurrent.*; public class agclassserver { public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub ArrayList<Integer> array = new ArrayList<Integer>(); ArrayList<Integer> arraySQR = new ArrayList<Integer>(); ArrayList<Integer> arrayFibo = new ArrayList<Integer>(); ArrayList<ClassesHandler> classes_handler = new ArrayList<ClassesHandler>(); ExecutorService service= Executors.newFixedThreadPool(10); boolean var=true; int message; try { ServerSocket server = new ServerSocket(9090); while(true) { System.out.println("I waiting for the client "); Socket socket = server.accept();// waiting for client to connect with server String hostName = socket.getInetAddress().getHostName(); ClassesHandler client = new ClassesHandler(socket,array,arraySQR,arrayFibo,hostName); classes_handler.add(client); service.execute(client); } }catch (Exception e) { } } }