Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object destroyed automatically in 20 seconds from Map

 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All.

I need to make a Map in which I will store a key value pair.but condition is that every object which is being stored should be destroyed automatically in 20 seconds.

please help me.

Thanks a Lot.

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you need two things to make this work.

1. The time the object is stored in the map needs to be stored somewhere. Maybe the key or value could contain that time, or maybe you could have a second map that is key => Date.

2. You need a thread that regularly checks the times, and removes key/values that have expired. (And make sure you synchronize access to the map when you do that!).

Does that make sense?
 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew for your reply.

But I am quite new to this(MultiThreading), so want some more help and clear Explanation or if possible then some code also .


Thanks a lot.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a key object which includes the timeI presume you will be able to work out how that class works. Make sure your Map supports this method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic