DKode
DKode

Reputation: 3

How do I perform Multithreading and Socket programming while sending an ArrayList to the thread in Java

Basically, i need to parse an XML file, put it into an ArrayList of class objects, and pass this (as well as a socket) to a thread. This thread will then do some functionality with the given ArrayList, and pass the result back to the socket. My problem is, I am not able to pass the parsed data in the form of an ArrayList to the thread.

Here is some code:

 public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    boolean listening = true;

  /*xml parsing is done, result is saved in ArrayList<class> peds*/

 try {

        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444." + e);
        System.exit(-1);
    }

    while (listening){ 
    Socket s = serverSocket.accept();
        Runnable proExec = new KKMultiServerThread( s, peds); **
        Thread th = new Thread (proExec)
        th.start(); }

    serverSocket.close();

    }


class KKMultiServerThread implements Runnable{
private Socket socket = null;
public final ArrayList<show> peds;

public KKMultiServerThread(Socket socket, ArrayList<show> peds) {

this.socket = socket;
    this.peds = peds;
        }

** The problem is occurring in this line. Says :non-static variable cannot be referenced from a static context.

have tried all possible combinations (such as placing the runnable class in another file, and calling a function to return the data structure.. the IDE does not allow me to do this.)

seems like the only possible remaining solution to this problem is to parse the xml file and save it in a data structure FOR EACH THREAD... this seems too costly to be efficient.

please help!

Upvotes: 0

Views: 367

Answers (3)

pens-fan-69
pens-fan-69

Reputation: 1039

The problem is that you aren't providing a local definition of peds when you construct your KKMultiServerThread. I suspect what you really want to do is something like the following:

while (listening){ 
    Socket s = serverSocket.accept();
    ArrayList<show> peds = getPedsFromSomewhere();
    Runnable proExec = new KKMultiServerThread(s, peds);
    Thread th = new Thread (proExec)
    th.start();
}

The compiler is complaining because the only definition of peds it can find is in the inner class KKMultiServerThread. This highlights the danger of overloading the usage of a particular name in a single class definition. If you pulled KKMultiServerThread into it's own top-level class, the problem would have become immediately apparent as you woudn't have had an overloaded usage of s and peds.

Upvotes: 0

stuartmclark
stuartmclark

Reputation: 1243

Take all of your code out of your main method and create a new class called ServerRunner or something. Then call ServerRunner.Start() and run all that code from there.

Upvotes: 0

Daniel Lundmark
Daniel Lundmark

Reputation: 2390

Your peds variable is not static, therefore it can not be accessed from a static context, it needs an object of the enclosing class.

Simplest solution is to make it static.

However, if you are logically going to need one ArrayList per request (in order to handle more than one request at a time) you should probably create a new ArrayList for each request and pass that to the Thread.

Upvotes: 1

Related Questions