Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
NUI
TULIP-CPS
Commits
8ec3cf4e
Commit
8ec3cf4e
authored
Nov 09, 2020
by
Nico Mack
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parent
3218642e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
123 additions
and
0 deletions
+123
-0
CPS/src/lu/list/itis/dkd/tui/utility/ThrottlingExecutor.java
CPS/src/lu/list/itis/dkd/tui/utility/ThrottlingExecutor.java
+123
-0
No files found.
CPS/src/lu/list/itis/dkd/tui/utility/ThrottlingExecutor.java
0 → 100644
View file @
8ec3cf4e
/**
* Copyright Luxembourg Institute of Science and Technology, 2020. All rights reserved. If you wish
* to use this code for any purpose, please contact the author(s).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package
lu.list.itis.dkd.tui.utility
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.TimerTask
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ScheduledFuture
;
import
java.util.concurrent.TimeUnit
;
/**
* @author nico.mack@list.lu
* @since 2.8
* @version 1.0.0
*/
// ***************************************************************************
// * Class Definition *
// ***************************************************************************
public
class
ThrottlingExecutor
{
private
long
last
;
private
ScheduledFuture
<?>
scheduled
;
private
long
coalescePeriod
;
private
static
ScheduledExecutorService
executor
=
Executors
.
newSingleThreadScheduledExecutor
();
// ***************************************************************************
// * Constants *
// ***************************************************************************
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ThrottlingExecutor
.
class
.
getSimpleName
());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
public
ThrottlingExecutor
()
{
this
.
coalescePeriod
=
0
;
this
.
last
=
0
;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Method for setting the minimum time between subsequent calls to this end point.
*
* @param millis
* The minimum number of milliseconds between subsequent calls to this end point.
*/
// ---------------------------------------------------------------------------
public
void
setCoalescePeriod
(
int
millis
)
{
this
.
coalescePeriod
=
millis
;
}
// ---------------------------------------------------------------------------
/**
* Checks whether this end point was previously called without being ready.
*
* @return <code>True</code> if end point was called without being ready to be called,
* <code>False</code> otherwise.
*/
// ---------------------------------------------------------------------------
public
boolean
isCallPending
()
{
return
(
scheduled
!=
null
);
}
// ---------------------------------------------------------------------------
public
void
submit
(
TimerTask
task
)
{
long
now
=
System
.
currentTimeMillis
();
if
((
this
.
coalescePeriod
>
0
)
&&
(
last
>
0
))
{
long
elapsed
=
now
-
last
;
if
(
elapsed
<
this
.
coalescePeriod
)
{
if
(
LOGGER
.
isDebugEnabled
())
{
LOGGER
.
debug
(
"Task submitted within {} ms coalesce period! Deferred execution in {} ms!"
,
this
.
coalescePeriod
,
(
this
.
coalescePeriod
-
elapsed
));
//$NON-NLS-1$
}
if
(
scheduled
!=
null
)
{
scheduled
.
cancel
(
true
);
if
(
LOGGER
.
isDebugEnabled
())
{
LOGGER
.
debug
(
"Cancelled previously scheduled task!"
);
//$NON-NLS-1$
}
}
scheduled
=
executor
.
schedule
(
task
,
(
this
.
coalescePeriod
-
elapsed
),
TimeUnit
.
MILLISECONDS
);
return
;
}
}
executor
.
schedule
(
task
,
0
,
TimeUnit
.
MILLISECONDS
);
last
=
now
;
scheduled
=
null
;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class
// ***************************************************************************
// ---------------------------------------------------------------------------
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment