Multi-threading Problem in Java: How to clear last element in a list when update method is no longer called?

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.

Up vote 0 down vote favorite share g+ share fb share tw.

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. Java multithreading server client link|improve this question asked Jun 22 '10 at 5:18Kyle254.

Without direct notification that the server is done/doesn't contain data, you need to leverage some other requirement or condition that can be used as a hint to clear the final element. You mention the server might send an update with the same data, does this mean that there is a requirement for it to update at a regular scheduled interval? – Tim Bender Jun 22 '10 at 5:48 The server is receiving data in certain situations, lets say radar data, for instance.

When it isn't receiving any though, it will not update, but as long as something is triggering said radar, the server will be sending data. – Kyle Jun 22 '10 at 6:27.

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 Jun 22 '10 at 6:28 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 Jun 22 '10 at 7:47.

In case oh this, use AtomicReference. The benefit of that can be found on this link ibm.com/developerworks/java/library/j-jt....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions