How to catch all the states of the thread and log them?
There is a code:
public static void main(String[] args) throws InterruptedException { Thread threadForLogging = new Thread(); LoggingStateThread loggingStateThread = new LoggingStateThread(threadForLogging); loggingStateThread.start(); threadForLogging.start(); threadForLogging.interrupted(); }
The LoggingStateThread is written as:
public class LoggingStateThread extends Thread { public Thread threadForLogging; public LoggingStateThread(Thread threadForLogging) { this.threadForLogging = threadForLogging; setDaemon(true); } @Override public void run() { State st; st = threadForLogging.getState(); System.out.println(st); do { if (!st.equals(threadForLogging.getState())) { st = threadForLogging.getState(); System.out.println(st); } } while (!st.equals(State.TERMINATED)); } }
I understand that the threads go in their own order. That's the question: how not to miss these states? This should be done by the LoggingStateThread class.
And yes, I understand that this can be done with the help of special libraries, but I want to figure out for myself how it works.