Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Java
|
Executors::newSingleThreadScheduledExecutor
Maven - POM Configuration
Example: ScheduledExecutorService::scheduleWithFixedDelay
Maven - POM Configuration
Before you can use the java concurrent API, you need to add some dependencies to your "
pom.xml
" file.
Here's the minimal dependencies needed in order for the following examples to work:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency>
Example: ScheduledExecutorService::scheduleWithFixedDelay
final long INITIAL_DELAY = 2l; final long DELAY = 2l; final String namingPattern = "_newSingleThreadScheduledExecutor"; final BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder(); final BasicThreadFactory basicThreadFactory = builder.namingPattern(namingPattern).daemon(true).build(); final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(basicThreadFactory); final Runnable runnable = () -> System.out.println(Thread.currentThread().getName() + ": Hello!"); scheduledExecutorService.scheduleWithFixedDelay(runnable, INITIAL_DELAY, DELAY, TimeUnit.MILLISECONDS); scheduledExecutorService.awaitTermination(10l, TimeUnit.MILLISECONDS); scheduledExecutorService.shutdown();
Output:
_newSingleThreadScheduledExecutor: Hello! _newSingleThreadScheduledExecutor: Hello! _newSingleThreadScheduledExecutor: Hello! _newSingleThreadScheduledExecutor: Hello! _newSingleThreadScheduledExecutor: Hello!
© 2010-2022
mti
tek