I'm writing a little Multi-threaded client-server Java application. When the server is started, it creates some objects like an object for db connection, another to manage users and so on. I need them to be accessed from every thread I start. Which is the right way to do that? Passing object istance to every Thread like this:
public class Server{
private ObjectType1 object1;
private ObjectType2 object2;
public void run{
.......
new ServerThread(object1,object2);
.......
}
}
or passing server istance to every Thread:
public class Server{
private TypeObject1 object1;
private TypeObject2 object2;
public TypeObject1 getObject1(){....}
public TypeObject2 getObject2(){...}
public void run(){
.....
new ServerThread(this);
.....
}
}
and then from the Thread access objects this way?
server.getObject1();