How to prevent an item from being used more than a certain count?
Trying to implement a ConcurrentQueue
that prevents an item from being used more than a specific # of times. For example, I want a User object that can be Dequeued 10 times, and after that, the user would be logged off and disposed.
Obviously fairly easy to implement by using a Dictionary
and keeping track of the number开发者_JAVA百科 of times each object has been used, in conjunction with the ConcurrentQueue
. However, just wondering if anybody has any neat ideas for managing this?
Something like this in java:
interface Dequeable
{
public int dequed();
}
class ConcurrentQueueImpl extends ConcurrentQueue
{
..................
.................
@override
public Dequeable deque()
{
Dequeable obj=.................;
**if(obj.dequed()>=10)**
{
// dispose off obj
return null;
}
return obj;
}
}
class SampleDequeableClass implements Dequeable
{
private int dequedCounter=0;
public int dequed()
{
dequedCounter++;
if(dequedCounter>=10)
{
// log off this object...
}
}
}
Create your own thread-safe Queue<T>
, just like the days without C#4.0. And in its Dequeue
method, record the use count. You needn't modify the original class/interface of the item. You can have another Dictionary<IFoo, int>
as a use counter.
精彩评论