Elvis
Elvis

Reputation: 857

Implement a monitor thread in Java

Well I am not familiar with threads in java, so I am dealing with this problem: I have a singleton object who contains some objects (let say sessions) and each object has a duration time, so that means that after a some time one object is considered expired so it needs to be removed from (a pool - List in singleton) singleton. To do this I decided to have a thread that checks every 5 minutes (or 10 minutes or whatever) and clean up all session in the singleton class. How can I implement such a functionality avoiding any possible deadlock and or time consuming blocks. Thank you in advance.

Upvotes: 2

Views: 2052

Answers (5)

wmorrison365
wmorrison365

Reputation: 6318

I agree with @quaylar (+1), use an existing caching technology, if you can.

If you can't, however, one solution is to use a java.util.Timer. Initialise it with the time till first session object expiry and put it to sleep. Then, on it awakening, have it remove your session object and reset it with the time to the next expiry time. Let java handle the timing aspects.

Upvotes: 2

Sushant
Sushant

Reputation: 1074

Well you can do something like this : I am assuming the class for your singleton object is called singleton so you have something like this (its not perfect code)

public class Singleton {
   List<Objects> singletonList = Collections.synchronizedList(new ArrayList<Objects>);



}

public class RemoveExpiredItemsThread implements Runnable {

    private Singleton singletonReference;
    private int sleepTime = 5*60*1000;
    // the constructor

    // then the run method which is something like this

    public void run() {
       while(done == false) {
         Thread.sleep(sleepTime);
         singletonReference.removeItem();
       }  
    }

}

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692081

I wouldn't implement it like that. Instead, I would delete the timed out sessions when a session is asked to the pool (not necessary at each get, though). This is, BTW, what is done by Guava's CacheBuilder, which you could use, since it's simple, tested, and provide useful features.

If you really want to go this way, then you should probably use a ConcurrentMap or ConcurrentList, and use a single-thread ScheduledExecutorService, which would wake iterate through the list and remove older sessions every X minutes.

Upvotes: 3

Kushan
Kushan

Reputation: 10703

Runnable cleaner = new Runnable() {
public void run() { /* remove expired objects here */ 
     //Using get method check whether object is expired 
   }
};
Executors.newScheduledThreadPool(1)
       .scheduleWithFixedDelay(cleaner, 0, 30, TimeUnit.SECONDS);

Upvotes: 2

quaylar
quaylar

Reputation: 2635

Is it an option for you to use a pre-existing in-memory caching-solution instead of writing your own? If yes you could check out Google Guava, which offers a Caching-Solution among many other things.

  1. See: Caching with Guava
  2. See: http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/cache/CacheBuilder.html

Upvotes: 2

Related Questions