Skip to content

Commit

Permalink
Refactor code for java-threads-example
Browse files Browse the repository at this point in the history
Refactor code for java-threads-example
  • Loading branch information
evrentan committed Aug 14, 2022
1 parent 51dae10 commit a74b9db
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion java-threads-example/src/MyThreadClassExtendsThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public MyThreadClassExtendsThread(String threadName) {
@Override
public void run() {
int amount = 0;
ThreadManagementUtility.printThreadEnd(this.getClass().getCanonicalName(), Thread.currentThread().getName(), Thread.currentThread().getState());
ThreadManagementUtility.printThreadStart(this.getClass().getCanonicalName(), Thread.currentThread().getName(), Thread.currentThread().getState());
ThreadManagementUtility.executeThread(amount, Thread.currentThread().getName(), Thread.currentThread().getState());
ThreadManagementUtility.printThreadEnd(this.getClass().getCanonicalName(), Thread.currentThread().getName(), Thread.currentThread().getState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ public class MyThreadClassImplementingRunnable implements Runnable {
@Override
public void run() {
int amount = 0;
ThreadManagementUtility.printThreadEnd(this.getClass().getCanonicalName(), Thread.currentThread().getName(), Thread.currentThread().getState());
ThreadManagementUtility.printThreadStart(this.getClass().getCanonicalName(), Thread.currentThread().getName(), Thread.currentThread().getState());
ThreadManagementUtility.executeThread(amount, Thread.currentThread().getName(), Thread.currentThread().getState());
ThreadManagementUtility.printThreadEnd(this.getClass().getCanonicalName(), Thread.currentThread().getName(), Thread.currentThread().getState());
}
Expand Down
14 changes: 12 additions & 2 deletions java-threads-example/src/ThreadManagementUtility.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadManagementUtility {

private ThreadManagementUtility() {
throw new IllegalStateException("Utility Class");
}

private static final int THREAD_SLEEP_IN_MILLIS = 1000;

protected void printThreadStart(String clazzName, String threadName, Thread.State threadState) {
private static final Logger logger = Logger.getLogger(ThreadManagementUtility.class.getCanonicalName());

protected static void printThreadStart(String clazzName, String threadName, Thread.State threadState) {
System.out.println(String.format("%s is starting for %s and thread state is %s !!!", clazzName, threadName, threadState));
}

Expand All @@ -18,7 +27,8 @@ protected static void executeThread(int amount, String threadName, Thread.State
System.out.println(String.format("**** Value of amount is %d from %s thread and thread status is %s ****", amount, threadName, threadState));
}
} catch (InterruptedException e) {
e.printStackTrace();
logger.log(Level.SEVERE, e.getMessage());
Thread.currentThread().interrupt();
}
}
}

0 comments on commit a74b9db

Please sign in to comment.