I don't understand the logic of lines 14-16 from the Solution class.
Why call Thread and not call logginstateThread.sleep?
Why when target.start is called the state of loggingStateThread changes ? Is it because loggingStateThread does not create a new thread in its constructor and it uses the target thread?
How is the terminated status achieved? Because there are no more methods called on the target thread?
Please ignore the code in the LoggingStateThread, I actually used the code from the mentor because I didn't know how to solve this.
public class LoggingStateThread extends Thread {
Thread thread;
LoggingStateThread (Thread thread){
this.thread = thread;
thread.setDaemon(true);
}
@Override
public void run() {
State currentState = thread.getState();
System.out.println(currentState);
State newState;
do {
if ((newState = thread.getState()) != currentState) {
currentState = newState;
System.out.println(newState);
}
} while (!currentState.equals(State.TERMINATED));
}
}
package com.codegym.task.task25.task2506;
/*
Monitoring thread state
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread target = new Thread();
LoggingStateThread loggingStateThread = new LoggingStateThread(target);
loggingStateThread.start(); // NEW
Thread.sleep(100);
target.start(); // RUNNABLE
Thread.sleep(100);
// TERMINATED
}
}