0

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) { } } } 
2
  • 1
    Why you don't print the caught exception in agclassserver class? Commented Oct 27, 2021 at 16:47
  • @ecerer , Oh my bad sorry , I was very confused so that I forgot to post . in addition I have solved the problem if you are interesed I could send you all the program . Commented Oct 28, 2021 at 18:12

1 Answer 1

1

Concurrent vs Parallel.

Imagine you need to get a new passport and you need to prepare a presentation for work.

Not parallel and not concurrent: first you go get your passport and then you prepare a presentation.

Not parallel, but concurrent: you go to the embassy and wait on a chair for you turn. While you are waiting, you work on your presentation. As soon as it is your turn, you close your laptop and fill in the passport papers and later you will complete your presentation.

Parallel and concurrent: you go work on your presentation while you send your wife/husband to the embassy to get your passport.

There is also a possibility to do parallel and not concurrent. But that doesn't fit with the above example. And example would calculating the sum of 2 huge integer arrays. Using SIMD multiple integer adds can be executed in parallel, but there is no concurrency since there is just a single request.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.