I am trying to make a client-server multithreading application. I have 3 classes:
- Client (GUI + Client: I/O socket stream) - 1. Thread
- Server (Server I/O socket stream + data manipulation) - 2. Thread
- ClientInfo (serializer class)
Everything works just as it must be. Except one thing. I want to have a static list in ClientInfo, that I can manipulate from both client/server through methods. Something like this:
private static volatile List<String> itemList = new ArrayList<String>();
public static synchronized List getItemList() {
return itemList;
}
synchronized methodForAdd()
synchronized methodForRemove()
//call from client/server method like this - ClientInfo.getItemList()
When I call these methods from the server thread it works properly, but when it must be called from client thread, method getItemList() is always empty, even in server calls there are more than 20 list elements.
My idea is to add elements in server thread in some condition and to remove an element in client thread in another condition. I tried everything that I found as a possible solutions but nothing worked.
What is the right way to do this and why in server there are many elements and in client is always empty when they are calling same static methods. Thanks.