my task is to create thread in this order: if A start->start B and C, if B start->start D. And destroy them in reverse order If D then B. If B and C then A. I hope you get it. I manage to do it but I guess there is better way to do it. Do you have any suggestions?
After your comments i have changed my code and it is much more simply. But now it looks "stupid". I would like to change hardcore of if statements and implementation, any advice? tnx for advice I'm learning with you.
This is my new code:
import java.util.*; class RobotController implements Runnable{ String name; public void run() { Thread t = Thread.currentThread(); System.out.println(t.getName() + " status = " + t.isAlive()); System.out.println(t.getName() + " status = " + t.getState()); } public static void main(String args[]) throws InterruptedException{ Thread thread_A = new Thread(new RobotController(), "Thread A"); Thread thread_B = new Thread(new RobotController(), "Thread B"); Thread thread_C = new Thread(new RobotController(), "Thread C"); Thread thread_D = new Thread(new RobotController(), "Thread D"); thread_A.start(); thread_A.join(); System.out.println(thread_A.getState()); thread_B.start(); thread_B.join(); System.out.println(thread_B.getState()); thread_C.start(); thread_C.join(); System.out.println(thread_C.getState()); thread_D.start(); System.out.println(thread_D.getState()); } }
Runnable), with a parent field of the same type, and boolean flag to kill it.if A start->start B and Cby this u mean afterAcompletes its work thenBstart in sequence..and afterBcompletes its work thenCstart ?