feat(ses): add scheduled-executor-service
This commit is contained in:
parent
c81976427b
commit
30df34ebac
|
@ -0,0 +1,6 @@
|
||||||
|
# Overview
|
||||||
|
This project can serve as example for asynchronous task scheduling with Maven and Java.
|
||||||
|
|
||||||
|
# Links
|
||||||
|
[Generating random numbers in Java](https://www.geeksforgeeks.org/generating-random-numbers-in-java/)
|
||||||
|
[https://www.geeksforgeeks.org/generating-random-numbers-in-java/](https://www.geeksforgeeks.org/scheduledexecutorservice-interface-in-java/)
|
|
@ -0,0 +1,86 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<name>ses</name>
|
||||||
|
<description>description</description>
|
||||||
|
<url>https://swingbe.de</url>
|
||||||
|
<groupId>de.swingbe</groupId>
|
||||||
|
<artifactId>ses</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>GNU General Public License</name>
|
||||||
|
<url>https://www.gnu.org/licenses/gpl-3.0.txt</url>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<url>https://github.com/Software-Ingenieur-Begerad/sandbox-java</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Other properties -->
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- Target Java versions -->
|
||||||
|
<release>11</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<filters>
|
||||||
|
<filter>
|
||||||
|
<!-- exclude signatures from merged JAR to avoid invalid signature messages -->
|
||||||
|
<artifact>*:*</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/*.SF</exclude>
|
||||||
|
<exclude>META-INF/*.DSA</exclude>
|
||||||
|
<exclude>META-INF/*.RSA</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
</filters>
|
||||||
|
<!-- The shaded JAR will not be the main artifact for the project, it will be attached
|
||||||
|
for deployment in the way source and docs are. -->
|
||||||
|
<shadedArtifactAttached>true
|
||||||
|
</shadedArtifactAttached>
|
||||||
|
<shadedClassifierName>shaded</shadedClassifierName>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<manifestEntries>
|
||||||
|
<Main-Class>de.swingbe.ses.SchedulerExecutorServiceExample
|
||||||
|
</Main-Class>
|
||||||
|
</manifestEntries>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,61 @@
|
||||||
|
package de.swingbe.ses;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//System.out.println();
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
class SchedulerExecutorServiceExample {
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
System.out.println(
|
||||||
|
"A count-down-clock program that counts from 10 to 0");
|
||||||
|
|
||||||
|
// creating a ScheduledExecutorService object
|
||||||
|
ScheduledExecutorService scheduler
|
||||||
|
= Executors.newScheduledThreadPool(11);
|
||||||
|
|
||||||
|
// printing the current time
|
||||||
|
System.out.println(
|
||||||
|
"Current time : "
|
||||||
|
+ Calendar.getInstance().get(Calendar.SECOND));
|
||||||
|
|
||||||
|
// Scheduling the tasks
|
||||||
|
for (int i = 10; i >= 0; i--) {
|
||||||
|
scheduler.schedule(new Task(i), 10 - i,
|
||||||
|
TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// remember to shutdown the scheduler
|
||||||
|
// so that it no longer accepts
|
||||||
|
// any new tasks
|
||||||
|
scheduler.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Task implements Runnable {
|
||||||
|
private int num;
|
||||||
|
public Task(int num) { this.num = num; }
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
if(num%1==0){
|
||||||
|
// create instance of Random class
|
||||||
|
Random rand = new Random();
|
||||||
|
// Generate random integers in range 0 to 1999
|
||||||
|
int randInt = rand.nextInt(2000);
|
||||||
|
|
||||||
|
System.out.println("num: " +num+", rand: " + randInt);
|
||||||
|
try {
|
||||||
|
Thread.sleep(randInt);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("Another thread has interrupted this one, message: " + e.getMessage() + ", trace: " + e.getStackTrace());
|
||||||
|
}
|
||||||
|
System.out.println("num: "+num+", sleep() done.");
|
||||||
|
}
|
||||||
|
System.out.println(
|
||||||
|
"num: " + num + " Current time : "
|
||||||
|
+ Calendar.getInstance().get(Calendar.SECOND));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue