Hey,
I am using org.apache.sling.commons.scheduler.Scheduler to run a job with
run() method, every 60 seconds.
Looking at log, it looks like there are 5 threads accessing run() method..
05.08.2011 11:18:46.084 *DEBUG* [pool-1-thread-1]
com.example.RemoteFeedService Finishing run() in RemoteFeedService
05.08.2011 11:18:46.084 *DEBUG* [pool-1-thread-5]
com.example.RemoteFeedService Starting run() in RemoteFeedService
05.08.2011 11:18:46.084 *DEBUG* [pool-1-thread-2]
com.example.RemoteFeedService Starting run() in RemoteFeedService
05.08.2011 11:18:46.085 *DEBUG* [pool-1-thread-3]
com.example.RemoteFeedService Starting run() in RemoteFeedService
05.08.2011 11:18:46.085 *DEBUG* [pool-1-thread-4]
com.example.RemoteFeedService Starting run() in RemoteFeedService
05.08.2011 11:18:46.085 *DEBUG* [pool-1-thread-3]
com.example.RemoteFeedService Starting run() in RemoteFeedService
05.08.2011 11:18:46.086 *DEBUG* [pool-1-thread-2]
com.example.RemoteFeedService Starting run() in RemoteFeedService
05.08.2011 11:19:03.928 *DEBUG* [pool-1-thread-5]
com.example.RemoteFeedService Starting run() in RemoteFeedService
I am registering the job to Scheduler as the following:
@Activate
private void activate(Map<String, ?> config) throws Exception {
isEnabled = OsgiUtil.toBoolean(config.get(IS_ENABLED), false);
schedPeriod = OsgiUtil.toLong(config.get(SCHEDULE_PERIOD), 0);
removeJob();
if (isEnabled && schedPeriod > 0) {
scheduler.addPeriodicJob(JOB_NAME, this, null, schedPeriod,
true);
}
}
private void removeJob() {
try {
scheduler.removeJob(JOB_NAME);
} catch (final NoSuchElementException e) {
LOG.info("No job to remove: " + JOB_NAME);
}
}
I want only one thread to run run() method every minute.
And, if previous run() haven't finished yet the next minute, it should not
run...
I tried synchronized:
public synchronized void run() {
...
But then it seems like there's deadlock...
1. why are there 5 threads accessing run() ?
2. how can i limit the number of threads for Scheduler (for this job only)?
3. how can I make run() method into critical section? (only one thread
executes it. while a thread is executing it, no other thread can execute
it..)