[04/45] tapestry-5 git commit: TAP5-2406: Use a single ReentrantLock for the PeriodicExecutor and its jobs
TAP5-2406: Use a single ReentrantLock for the PeriodicExecutor and its jobs Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/32571619 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/32571619 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/32571619 Branch: refs/heads/beanmodel-split Commit: 3257161990f639eabb60ce8729e3c1244823609a Parents: 57a8d25 Author: Howard M. Lewis Ship Authored: Fri Oct 24 13:59:37 2014 -0700 Committer: Howard M. Lewis Ship Committed: Fri Oct 24 13:59:37 2014 -0700 -- .../services/cron/PeriodicExecutorImpl.java | 181 +-- 1 file changed, 121 insertions(+), 60 deletions(-) -- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/32571619/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java -- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java index abfcde9..cf3c717 100644 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java +++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java @@ -1,5 +1,3 @@ -// Copyright 2011 The Apache Software Foundation -// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -26,6 +24,8 @@ import org.slf4j.Logger; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; public class PeriodicExecutorImpl implements PeriodicExecutor, Runnable { @@ -33,18 +33,19 @@ public class PeriodicExecutorImpl implements PeriodicExecutor, Runnable private final Logger logger; -// Synchronized by this +// Synchronized by jobLock private final List jobs = CollectionFactory.newList(); private final Thread thread = new Thread(this, "Tapestry PeriodicExecutor"); -// Synchronized by this. Set when the registry is shutdown. -private boolean shutdown; +private transient boolean shutdown; private static final long FIVE_MINUTES = 5 * 60 * 1000; private final AtomicInteger jobIdAllocator = new AtomicInteger(); +private final Lock jobLock = new ReentrantLock(); + private class Job implements PeriodicJob, Invokable { final int jobId = jobIdAllocator.incrementAndGet(); @@ -74,43 +75,71 @@ public class PeriodicExecutorImpl implements PeriodicExecutor, Runnable return name; } -public synchronized long getNextExecution() +public long getNextExecution() { -return nextExecution; +try +{ +jobLock.lock(); +return nextExecution; +} finally +{ +jobLock.unlock(); +} } @Override -public synchronized boolean isExecuting() +public boolean isExecuting() { -return executing; +try +{ +jobLock.lock(); +return executing; +} finally +{ +jobLock.unlock(); +} } @Override -public synchronized boolean isCanceled() +public boolean isCanceled() { -return canceled; +try +{ +jobLock.lock(); +return canceled; +} finally +{ +jobLock.unlock(); +} } @Override -public synchronized void cancel() +public void cancel() { -canceled = true; +try +{ +jobLock.lock(); + +canceled = true; + +if (!executing) +{ +removeJob(this); +} -if (!executing) +// Otherwise, it will be caught when the job finishes execution. +} finally { -removeJob(this); +jobLock.unlock(); } - -// Otherwise, it will be caught when the job finishes execution. } @Override -public synchronized String toString() +public String toString() { StringBuilder builder = new StringBuilder("PeriodicJob[#").append(jobId); - builder.append(", (
git commit: TAP5-2406: Use a single ReentrantLock for the PeriodicExecutor and its jobs
Repository: tapestry-5 Updated Branches: refs/heads/master 57a8d25dd -> 325716199 TAP5-2406: Use a single ReentrantLock for the PeriodicExecutor and its jobs Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/32571619 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/32571619 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/32571619 Branch: refs/heads/master Commit: 3257161990f639eabb60ce8729e3c1244823609a Parents: 57a8d25 Author: Howard M. Lewis Ship Authored: Fri Oct 24 13:59:37 2014 -0700 Committer: Howard M. Lewis Ship Committed: Fri Oct 24 13:59:37 2014 -0700 -- .../services/cron/PeriodicExecutorImpl.java | 181 +-- 1 file changed, 121 insertions(+), 60 deletions(-) -- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/32571619/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java -- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java index abfcde9..cf3c717 100644 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java +++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/cron/PeriodicExecutorImpl.java @@ -1,5 +1,3 @@ -// Copyright 2011 The Apache Software Foundation -// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -26,6 +24,8 @@ import org.slf4j.Logger; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; public class PeriodicExecutorImpl implements PeriodicExecutor, Runnable { @@ -33,18 +33,19 @@ public class PeriodicExecutorImpl implements PeriodicExecutor, Runnable private final Logger logger; -// Synchronized by this +// Synchronized by jobLock private final List jobs = CollectionFactory.newList(); private final Thread thread = new Thread(this, "Tapestry PeriodicExecutor"); -// Synchronized by this. Set when the registry is shutdown. -private boolean shutdown; +private transient boolean shutdown; private static final long FIVE_MINUTES = 5 * 60 * 1000; private final AtomicInteger jobIdAllocator = new AtomicInteger(); +private final Lock jobLock = new ReentrantLock(); + private class Job implements PeriodicJob, Invokable { final int jobId = jobIdAllocator.incrementAndGet(); @@ -74,43 +75,71 @@ public class PeriodicExecutorImpl implements PeriodicExecutor, Runnable return name; } -public synchronized long getNextExecution() +public long getNextExecution() { -return nextExecution; +try +{ +jobLock.lock(); +return nextExecution; +} finally +{ +jobLock.unlock(); +} } @Override -public synchronized boolean isExecuting() +public boolean isExecuting() { -return executing; +try +{ +jobLock.lock(); +return executing; +} finally +{ +jobLock.unlock(); +} } @Override -public synchronized boolean isCanceled() +public boolean isCanceled() { -return canceled; +try +{ +jobLock.lock(); +return canceled; +} finally +{ +jobLock.unlock(); +} } @Override -public synchronized void cancel() +public void cancel() { -canceled = true; +try +{ +jobLock.lock(); + +canceled = true; + +if (!executing) +{ +removeJob(this); +} -if (!executing) +// Otherwise, it will be caught when the job finishes execution. +} finally { -removeJob(this); +jobLock.unlock(); } - -// Otherwise, it will be caught when the job finishes execution. } @Override -public synchronized String toString() +public String toString() { StringBuilder builder = new Strin