I have a problem. I suppose this has something to do with concurrency or thread synchronization, although I can't put my finger on just what is happening.
Here is my description of the data flow for our FriendRequestList object.
On the client side, we send a friendโs request to another user (employee). Therefore, we send a request and add our own username to our User.incFriendReq -list. Instance is an ArrayList for reference only.
So, now we send a request to the server to get our own friend request list ( FriendRequestList.java ).
So now the problem. If I use this code below, the user will not see the friendโs request before he stops his connection (logout), which closes the connection. When he logs in, he will only see the request in his list.
Server Side Code:
... Worker.java ...
private Object getFriendRequests() { User me = Data.getUser(myUserName); if ( me == null ) { return new NoSuchUserException();
... User.java ...
private List<String> incFriendReq; public List<String> getFriendReq() { return incFriendReq; }
Client side
... Communication.java ...
public FriendRequestList getRequests() { sendObject(new GetRequests()); return inputHandler.containsRequests(); }
... MessageListener.java ...
public void run() { ... FriendRequestList requests = communication.getRequests(); update(requestList, requests);
However, if I update Worker.java to do this instead:
private Object getFriendRequests() { User me = Data.getUser(myUserName); if ( me == null ) { return new NoSuchUserException(); } return new FriendList(me.getFriends().stream().collect(Collectors.toList())); }
At that moment, when another user asks for my friendship, I see a request in my list. What gives? It sounds to me just like the underlying data structure is not being updated, race conditions or something else. But the fix is โโhow I retrieve server-side data using a stream.
Please explain to someone how it will be done in Java 7 before threads solve my interesting problem for me.
On note
I want to add that users are placed inside LinkedBlockingDeque and retrieve a share for workers from the Data object.