
net.sf.eBus.timer.EScheduledExecutorAbstractSpin Maven / Gradle / Ivy
//
// Copyright 2024 Charles W. Rapp
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package net.sf.eBus.timer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import net.sf.eBus.config.EConfigure;
/**
* Base class for {@link EScheduledExecutorSpinning},
* {@link EScheduledExecutorSpinSleep}, and
* {@link EScheduledExecutorSpinYield} scheduled executor types.
* Contains spin limit setting and update flag common to all
* three types. Also defines {@link #addTimer(ETimerImpl)},
* {@link #removeTimer(ETimerImpl)}, and
* {@link #signalShutdown()} abstract methods.
*
* @author Charles W. Rapp
*/
/* package */ abstract class EScheduledExecutorAbstractSpin
extends EScheduledExecutor
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Locals.
//
/**
* Spin this many times waiting for a timer to expire before
* parking the thread. Used only by spin+park and spin+sleep
* thread types.
*/
protected final long mSpinLimit;
/**
* Set to {@code true} when {@link #mTimers timers set) is
* updated (either a new timer is added or an existing timer
* removed).
*/
protected final AtomicBoolean mUpdateFlag;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a spinning scheduled executor based on the given
* configuration.
* @param config contains scheduled executor configuration.
* @param startupSignal decrement signal when scheduled
* executor thread is up and running.
* @param shutdownSignal decrement signal when scheduled
* executor thread is shut down.
*/
protected EScheduledExecutorAbstractSpin(final EConfigure.ScheduledExecutor config,
final CountDownLatch startupSignal,
final CountDownLatch shutdownSignal)
{
super (config, startupSignal, shutdownSignal);
mUpdateFlag = new AtomicBoolean();
mSpinLimit = config.spinLimit();
} // end of EScheduledExecutorAbstractSpin(...)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Abstract Method Implementations.
//
@Override
protected final void addTimer(final ETimerImpl timer)
{
// Since the timers set is not locked, there is no way to
// know if this new timer is the next to expire or
// another timer being added simultaneously. This being
// the case, add the timer and set the update flag.
mTimers.add(timer);
mUpdateFlag.set(true);
// Interrupt this thread in case it is parked.
this.interrupt();
} // end of addTimer(ETimerImpl)
@Override
protected final void removeTimer(final ETimerImpl timer)
{
mTimers.remove(timer);
mUpdateFlag.set(true);
// Interrupt this thread in case it is parked.
this.interrupt();
} // end of removeTimer(ETimerImpl)
//
// end of Abstract Method Implementations.
//-----------------------------------------------------------
} // end of class EScheduledExecutorAbstractSpin
© 2015 - 2025 Weber Informatics LLC | Privacy Policy