Skip to content

Commit

Permalink
Changed Scheduler class to be a enum and modified the way how Singlet…
Browse files Browse the repository at this point in the history
…on instance is implemented
  • Loading branch information
Wagner Franchin committed May 13, 2018
1 parent f9ce2da commit 47b1ce1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaTaskScheduler</name>
<name>Java-TaskScheduler</name>
<comment></comment>
<projects>
</projects>
Expand Down
3 changes: 1 addition & 2 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/scheduler/
/factory/
/main/
/scheduler/
/task/
6 changes: 3 additions & 3 deletions src/factory/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void executeMethod() throws Exception {
print_task.append(this.getClass().getSimpleName()).append("-ID:").append(id).append("]");

print_task = sFather.append(print_task);
System.out.println(Util.getCurrentHour() + " S " + print_task.toString() + " started -> CPU: " + Util.getProcessCPU() + " (Queue Size: " + Scheduler.getInstance().getQueueSize() + ")");
System.out.println(Util.getCurrentHour() + " S " + print_task.toString() + " started -> CPU: " + Util.getProcessCPU() + " (Queue Size: " + Scheduler.INSTANCE.getQueueSize() + ")");
long start = System.currentTimeMillis();
String future = (String) execute(this);
long end = System.currentTimeMillis();
Expand All @@ -61,7 +61,7 @@ private void executeMethod() throws Exception {
Thread.sleep(delay);

for (Task dependecy : queuedDependecy) {
Scheduler.getInstance().addQueue(dependecy);
Scheduler.INSTANCE.addQueue(dependecy);
}
}

Expand Down Expand Up @@ -188,7 +188,7 @@ public void run() {
try {

Thread.sleep(task.getFixedRate());
Scheduler.getInstance().addQueue(task);
Scheduler.INSTANCE.addQueue(task);

} catch (InterruptedException e) {
bstop = true;
Expand Down
10 changes: 8 additions & 2 deletions src/main/MainScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ public static void main(String args[]) throws InterruptedException {

Util.loadProperties();
//Starting the Scheduler thread
new Thread(Scheduler.getInstance(), "shceduler_thread").start();
Runnable runScheduler = new Runnable() {
@Override
public void run() {
Scheduler.INSTANCE.run();
}
};
new Thread(runScheduler, "scheduler_thread").start();

//create tasks
for(String name : Util.tasks){
try {
Task t = TaskFactory.create(name);
if (t != null)
Scheduler.getInstance().addQueue(t);
Scheduler.INSTANCE.addQueue(t);

} catch (Exception e) {
System.err.println("Some problem while creating tasks. Exception message: " + e.getMessage());
Expand Down
22 changes: 6 additions & 16 deletions src/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,18 @@
import factory.Task;
import factory.Util;

final public class Scheduler implements Runnable {
/**
* Singleton class using enum
*
*/
public enum Scheduler {
INSTANCE;

private static int taskID = 0;

private Queue<Task> queue;
private boolean bStop;

/**
* SINGLETON -----------------------------------------------------
*/
private static class SchedulerSingleton {
private static final Scheduler INSTANCE = new Scheduler();
}

public static Scheduler getInstance() {
return SchedulerSingleton.INSTANCE;
}
/**
* ----------------------------------------------------------------
*/

private Scheduler() {
queue = new LinkedList<Task>();
bStop = false;
Expand All @@ -50,7 +41,6 @@ public void addQueue(Task task) throws InterruptedException {
}
}

@Override
public void run() {

try {
Expand Down

0 comments on commit 47b1ce1

Please sign in to comment.