views:

55

answers:

2

I have this problem in a project I'm working on:

I have a list of elements sent through a server, I have a local copy of that list each time the server calls my client's update method.

The update method itself checks if the newly sent list is identical to the local copy, if not, modify the local copy. This means that if on the server, the element is no longer present, then it should be removed from the local copy.

The problem arises when the last element disappears on the server: once it does, the server stops calling the update method. This means that the local copy is left with a single element until the update method is called again, since it only gets called while the server has data to send.

Now I can't change the server architecture or the fact that it doesn't call the method when the list is empty. I can only modify the client side.

I've been experimenting with threads on my client to have some sort of thread running in parallel to clear the list if the list has not been updated in a while. This has not been working correctly or effectively, and I seem to get random results that can change completely just by changing how often the thread runs.

My main problem arises because I can't seem to find an effective way to tell if I am actively receiving data or not. I also can't seem to find a way to check if a thread is currently blocked or not.

Any ideas on how I can approach this problem? I'm not very good with threading/multi-threading and I've been bashing my head against this wall for a while now.

+1  A: 

Seeing some code would help in providing a more specific answer, but if the approach you've pursued works on a conceptual level, you could take a look at Java's Timer class and create a TimerTask that calls a method that checks when the list was last updated and clears it if appropriate.

Lauri Lehtinen
I had just tried my own sort of timer thread and it seemed to be doing the job, but bogging up my CPU by 100% :/ I'm going to try the java Timer class, it probably does it more efficiently then I can.
Kyle
Hah okay, the timer class was much more efficient!What I was doing before is that I started the thread inside the update method, now I start the timer outside and then close it separately and everything works dandy. Thanks!
Kyle
A: 

In case oh this, use AtomicReference.the benefit of that can be found on this link http://www.ibm.com/developerworks/java/library/j-jtp11234/

Shashank T