Parallel Radix Sorting client/server
I'm having an issue with the way my client and server are communicating. When I use a threaded client to create 10 threads (makes 10 clients) 3 out of 10 come up scrambled, 7 out of 10 come up perfect!
I need some help. I've been on this for over 2 weeks!
SERVER
Code:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ParallelRadixSort implements Runnable {
private static Vector connectionSocket = new Vector();
private static Thread[] threadarray = new Thread[10];
private static int index=0;
private int i=index;
private static ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
private static Stack RadixStack = new Stack<Integer>();
public static void main(String argsv[]) throws Exception {
ServerSocket welcomeSocket = new ServerSocket(16788);
int largest = 0;
for(int i=0; i<1000; i++){
RadixStack.push((int)(Math.random()*1000));
if((int)RadixStack.peek()>largest)
largest=(int)RadixStack.peek();
}
for(int i=0; i<10; i++){
arraystack.add(new Stack<Integer>());
}
int largestValue = Integer.toString(largest).length()-1;
stackAssign(largestValue, RadixStack);
while(true){
if(index<10){
System.out.println("Server: Listening for client request.");
connectionSocket.add(welcomeSocket.accept());
System.out.println("Server: Connection Established with client(s)");
threadarray[index] = (new Thread(new ParallelRadixSort()));
threadarray[index].start();
System.out.println("creating thread "+ index);
index++;
}
else{
for(int j=0; j<10; j++){
System.out.println("joining thread "+j);
threadarray[j].join();
}
System.out.println("thread joining complete!");
for(int k=0; k<10; k++){
System.out.println(" \n Bucket "+k+":");
while(!arraystack.get(k).empty()){
System.out.print(arraystack.get(k).pop()+", ");
}
System.out.println();
}
break;
}
}
}
public static void stackAssign(int placeValue, Stack<Integer> RadixStack){
while(!RadixStack.empty()){
int Data = RadixStack.pop();
int X = (int) (Data/Math.pow(10,placeValue)%10);
arraystack.get(X).push(Data);
System.out.println("Bucket "+X+": "+Data);
}
}
public void run() {
//Create an input stream to read from the socket at slot i of the array
try{
Socket S = (Socket) connectionSocket.lastElement();
connectionSocket.remove(S);
ObjectOutputStream out = new ObjectOutputStream(S.getOutputStream());
out.writeObject(arraystack.get(i));
ObjectInputStream in = new ObjectInputStream(S.getInputStream());
try {
Stack<Integer> RadixDATA = (Stack<Integer>) in.readObject();
arraystack.add(i, RadixDATA);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ParallelRadixSort.class.getName()).log(Level.SEVERE, null, ex);
}
out.close();
in.close();
S.close();
System.out.println("Server: Done with client thread "+i);
}
catch(IOException ioe){
System.out.println("IO failed");
}
}
}
CLIENT
Code:
import java.util.*;
import java.net.*;
import java.io.*;
public class RadixSortClient implements Runnable {
private static Vector connectionSocket = new Vector();
private static int index=0;
private int i=index;
public static void main(String argsv[]) throws Exception {
while(index<10){
System.out.println("Client: starting MultiClient thread(s)!");
(new Thread(new RadixSortClient())).start();
index++;
}
}
public static Stack<Integer> RadixSort(Stack<Integer> RadixDATA){
ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
Stack RadixStack = new Stack<Integer>();
int largest=0;
int largestValue=0;
int Data=0;
int X=0;
while(!RadixDATA.empty()){
RadixStack.push(RadixDATA.pop());
if((int)RadixStack.peek()>largest)
largest=(int)RadixStack.peek();
}
largestValue = Integer.toString(largest).length();
// create array of stacks
for (int i=0; i<10; i++){
arraystack.add(new Stack<Integer>());
}
for(int placeValue=0; placeValue<largestValue; placeValue++){
//sort numbers in array of stacks
while(!RadixStack.empty()){
Data=(int)RadixStack.pop();
X = (int) (Data/Math.pow(10,placeValue)%10);
arraystack.get(X).push(Data);
}
//push back to single stack
for(int k=0; k<10; k++){
while(!arraystack.get(k).empty()){
RadixStack.push((arraystack.get(k).pop()));
}
}
}
return RadixStack;
}
public void run(){
try{
System.out.println("Client: Connecting to server....");
Socket ConnectionSocket = new Socket("localhost",16788);
System.out.println("Client "+i+" : Connection Esablished!");
Stack RadixDATA = new Stack<Integer>();
ObjectInputStream in = new ObjectInputStream(ConnectionSocket.getInputStream());
//Object capture and Radix Sort Code here
try{
RadixDATA = (Stack<Integer>) in.readObject();
}
catch(Exception e){
System.out.println("Stack capture failed!");
}
try{
RadixDATA = RadixSort(RadixDATA);
}
catch(Exception e){
System.out.println("RadixSort Failed!");
}
//create and output stream to the client socket
ObjectOutputStream out = new ObjectOutputStream(ConnectionSocket.getOutputStream());
out.writeObject(RadixDATA);
System.out.println("Client "+i+" is done writing");
in.close();
out.close();
ConnectionSocket.close();
System.out.println("Client: Done with server thread "+i);
}
catch(IOException ioe){
System.out.println("IO failed");
}
}
}
Radix Sort Algorithm (just for reference, if anyone wants it)
Code:
import java.util.*;
public class RadixSortingAlgorithm {
public static void main(String argsv[]){
ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
Stack RadixStack = new Stack<Integer>();
int largest=0;
int largestValue=0;
int Data=0;
int X=0;
for(int i=0; i<1000; i++){
RadixStack.push((int)(Math.random()*1000));
if((int)RadixStack.peek()>largest)
largest=(int)RadixStack.peek();
}
largestValue = Integer.toString(largest).length();
// create array of stacks
for (int i=0; i<10; i++){
arraystack.add(new Stack<Integer>());
}
for(int placeValue=0; placeValue<largestValue; placeValue++){
//sort numbers in array of stacks
while(!RadixStack.empty()){
Data=(int)RadixStack.pop();
X = (int) (Data/Math.pow(10,placeValue)%10);
arraystack.get(X).push(Data);
}
//push back to single stack
for(int k=0; k<10; k++){
while(!arraystack.get(k).empty()){
RadixStack.push((arraystack.get(k).pop()));
}
}
}
while(!RadixStack.empty()){
System.out.print(RadixStack.pop()+" ,");
}
}
}