Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
- public class LoggingStateThread extends Thread{
- Thread thread;
- LoggingStateThread(Thread thread){
- this.thread = thread;
- this.setDaemon(true);
- }
- @Override
- public void run() {
- System.out.println(thread.getState());
- Thread.State pastState = thread.getState();
- super.run();
- while (!thread.getState().equals(State.TERMINATED)){
- State state = thread.getState();
- if (!state.equals(pastState)){
- System.out.println(state);
- pastState = state;
- }
- }
- System.out.println(thread.getState());
- }
- }
Add Comment
Please, Sign In to add comment