feat(task_periodic): added manual task repetition
This commit is contained in:
parent
7c62b4a95c
commit
c809a772a8
|
@ -0,0 +1,48 @@
|
|||
package de.swingbe.task_periodic;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class LineNoCounter {
|
||||
|
||||
public static void count() {
|
||||
System.out.println("count started...");
|
||||
|
||||
File file = null;
|
||||
try {
|
||||
file = new File("/opt/npm/NetPeerManager.log");
|
||||
|
||||
} catch (NullPointerException e) {
|
||||
System.out.println("path name arg is null");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
FileInputStream fileInputStream = null;
|
||||
try {
|
||||
fileInputStream = new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("file not found");
|
||||
e.printStackTrace();
|
||||
}
|
||||
InputStreamReader inputStreamReader = null;
|
||||
inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.ISO_8859_1);
|
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
||||
|
||||
String line;
|
||||
int lineCount = 0;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
if (!((line = bufferedReader.readLine()) != null)) break;
|
||||
} catch (IOException e) {
|
||||
System.out.println("I/O error occured");
|
||||
e.printStackTrace();
|
||||
}
|
||||
lineCount++;
|
||||
}
|
||||
|
||||
System.out.println("lineCount = " + lineCount);
|
||||
System.out.println("count done.");
|
||||
|
||||
}
|
||||
}
|
|
@ -1,12 +1,68 @@
|
|||
package de.swingbe.task_periodic;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.io.BufferedReader;
|
||||
|
||||
import static de.swingbe.task_periodic.LineNoCounter.count;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
public static BufferedReader bufferedReader = null;
|
||||
|
||||
private static void doNothing() {
|
||||
System.out.println("doNothing");
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Another thread has interrupted this one");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void justDoIt() {
|
||||
System.out.println("justDoIt started...");
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Another thread has interrupted this one");
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("justDoIt done.");
|
||||
return;
|
||||
}
|
||||
|
||||
private static void doWhileTrue() {
|
||||
System.out.println("doWhileTrue started...");
|
||||
while (true) {
|
||||
//TODO clean up justDoIt();
|
||||
doNothing();
|
||||
}
|
||||
}
|
||||
|
||||
private static void doForI() {
|
||||
System.out.println("doForI started...");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
System.out.println("Execution in Main Thread...." + i);
|
||||
justDoIt();
|
||||
}
|
||||
System.out.println("doForI done.");
|
||||
return;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("main started...");
|
||||
|
||||
while (true) {
|
||||
count();
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Another thread has interrupted this one");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*TODO clean up
|
||||
//instantiate Timer object
|
||||
Timer time = new Timer();
|
||||
|
||||
|
@ -17,19 +73,9 @@ public class Main {
|
|||
time.schedule(st, 0, 1000);
|
||||
|
||||
//for demo only.
|
||||
for (int i = 0; i < 5; i++) {
|
||||
System.out.println("Execution in Main Thread...." + i);
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Another thread has interrupted this one");
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (i == 4) {
|
||||
System.out.println("Application Terminates");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
return;
|
||||
//TODO clean updoForI();
|
||||
doWhileTrue();
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,18 +3,34 @@ package de.swingbe.task_periodic;
|
|||
import java.util.Date;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static de.swingbe.task_periodic.LineNoCounter.count;
|
||||
|
||||
public class ScheduledTask extends TimerTask {
|
||||
|
||||
//to display current time
|
||||
Date now;
|
||||
private static void showDate() {
|
||||
|
||||
//ddd your task here
|
||||
public void run() {
|
||||
//to display current time
|
||||
Date now;
|
||||
|
||||
//initialize date
|
||||
now = new Date();
|
||||
|
||||
//display current time
|
||||
System.out.println("Time is :" + now);
|
||||
|
||||
}
|
||||
|
||||
private static void showLineNoCount() {
|
||||
System.out.println("showLineNoCount started...");
|
||||
count();
|
||||
System.out.println("showLineNoCount done.");
|
||||
}
|
||||
|
||||
//do your task here
|
||||
public void run() {
|
||||
|
||||
//TODO clean up showDate();
|
||||
|
||||
showLineNoCount();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue