You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by sam ” <sk...@gmail.com> on 2011/08/05 17:31:43 UTC

scheduler thread pool?

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..)

Re: scheduler thread pool?

Posted by sam ” <sk...@gmail.com>.
oh i'm so stupid.

Thank you. Should've read .addPeriodicJob() documentation instead of
copy-paste the example from
http://sling.apache.org/site/scheduler-service-commons-scheduler.html


Thanks!!

On Fri, Aug 5, 2011 at 1:32 PM, Carsten Ziegeler <cz...@apache.org>wrote:

> Hi,
>
> the solution to your problem is easy: change the last argument when
> you add your job from 'true' to 'false'. This boolean instructs the
> scheduler if a job can run concurrently. If false, the scheduler will
> only start a new job if the previous has finished.
> If you schedule a job to run every minute but your job takes longer
> than 1 minute, than you run into this potential problem - but with
> canRunConcurrently set to 'false' this is nicely handled for you
>
> Regards
> Carsten
>
> 2011/8/5 sam ” <sk...@gmail.com>:
> > 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..)
> >
>
>
>
> --
> Carsten Ziegeler
> cziegeler@apache.org
>

Re: scheduler thread pool?

Posted by Carsten Ziegeler <cz...@apache.org>.
Hi,

the solution to your problem is easy: change the last argument when
you add your job from 'true' to 'false'. This boolean instructs the
scheduler if a job can run concurrently. If false, the scheduler will
only start a new job if the previous has finished.
If you schedule a job to run every minute but your job takes longer
than 1 minute, than you run into this potential problem - but with
canRunConcurrently set to 'false' this is nicely handled for you

Regards
Carsten

2011/8/5 sam ” <sk...@gmail.com>:
> 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..)
>



-- 
Carsten Ziegeler
cziegeler@apache.org