0

I would like to start off by saying that if you know a better way to do this, please tell me. I would rather know a better way of doing this than doing it incorrectly.

I am writing a server program that has to interact with multiple clients. I am using the method that this answer proposed. (socket programming multiple client to one server). I am creating the new Instance of the 'EchoThread'.

I also have another class that has a number of variables that need to be able to be accessed and changed by ALL the classes / operations in my server. I am wondering how I inject (I think that is what it's called) the Instance of that from the class creating the new 'EchoThread' object and the 'EchoThread' Class.

Part of the answer from the link above:

 while (true) {
        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("I/O error: " + e);
        }
        // new threa for a client
        new EchoThread(socket).start();
    }

I have the Instance of the class I want to inject:

VarsClass vars = new VarsClass();

I normally "link" these by doing thing:

VarsClass vars = new VarsClass();
ExampleClass example = new ExampleClass();
example.link(vars);

Here is the 'link' method in the 'ExampleClass'

public void setGlobalVariables(GlobalVariables global) {

    this.globalVariables = global;

}

How do I reference the 'EchoThread' like I do in the above example with 'ExmapleClass' and 'VarsClass'? I am doing this, so each class is not creating a new instance of the VarsClass. (Avoiding the problem I had here: Using the same variables in two threads, but I don't know why they won't update in Java)

Thank you to every one in advance for your patience and help. ~Rane

Community
  • 1
  • 1
Rane
  • 191
  • 2
  • 13
  • You can possibly inject here: `new EchoThread(socket).start();`. Give EchoThread's constructor another parameter or more. – Hovercraft Full Of Eels Oct 04 '14 at 00:51
  • Don't write code like this. The line that starts the thread should be inside the try block: otherwise, if there is an exception, you will start another thread using the last-accepted socket. Code that depends on the success of code in a try block should be in the try block too. – user207421 Oct 04 '14 at 02:17

2 Answers2

1

How do I reference the EchoThread ...

The simple way is like this:

EchoThread et = new EchoThread(socket);
et.link(vars);
et.start();

Or pass the vars object as another argument to the constructor.


Note that the following variant is wrong ... unless you make EchoThread thread-safe.

EchoThread et = new EchoThread(socket);
et.start();
...
et.link(vars);

(It is safe to pass the argument prior to the start() call because there is an explicit "happens before" on the start() call ... between the thread that calls start() and the starting of new thread's run() method.)


The other thing to note is that writing subclasses of Thread is generally considered to be a poor way of implementing multi-threading. A better way is to implement your thread's logic as a Runnable; e.g.

public class MyRunnable implements Runnable {
    // thread state variables here ...
    public MyRunnable(Socket sock, GlobalVariables vars) {
         ...
    }

    public void run() {
         // thread logic goes here ...
    }
}

and the use it like this:

new Thread(new MyRunnable(sock, vars)).start();

This approach allows you to easily change your code to use a thread pool or an Executor instead of a Thread that you create on the fly. (Creating threads on the fly tends to be expensive.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You can do it the same way as with instances of any other class.

EchoThread thread = new EchoThread(socket);
thread.setGlobalVariables(globalVars);
thread.start();
user253751
  • 57,427
  • 7
  • 48
  • 90