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,35 +1,81 @@ |
||||
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; |
||||
|
||||
//instantiate Timer object
|
||||
Timer time = new Timer(); |
||||
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(); |
||||
} |
||||
} |
||||
|
||||
//instantiate ScheduledTask class
|
||||
ScheduledTask st = new ScheduledTask(); |
||||
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; |
||||
} |
||||
|
||||
//create repetitively task for every 1 secs
|
||||
time.schedule(st, 0, 1000); |
||||
private static void doWhileTrue() { |
||||
System.out.println("doWhileTrue started..."); |
||||
while (true) { |
||||
//TODO clean up justDoIt();
|
||||
doNothing(); |
||||
} |
||||
} |
||||
|
||||
//for demo only.
|
||||
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(2000); |
||||
Thread.sleep(500); |
||||
} 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 up |
||||
//instantiate Timer object
|
||||
Timer time = new Timer(); |
||||
|
||||
//instantiate ScheduledTask class
|
||||
ScheduledTask st = new ScheduledTask(); |
||||
|
||||
//create repetitively task for every 1 secs
|
||||
time.schedule(st, 0, 1000); |
||||
|
||||
//for demo only.
|
||||
//TODO clean updoForI();
|
||||
doWhileTrue(); |
||||
*/ |
||||
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue