synchronized thread execution

后端 未结 4 970
终归单人心
终归单人心 2021-01-22 17:27

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.

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-22 18:10

    There are some flaws in your code which will make it not to work accordingly sometimes:

    1. You called thread_A.start() and then checked thread_A.isAlive(). Now what if , thread_A is already completed before thread_A.isAlive() condition is checked?.thread_B and thread_C is never started. Your application fails.
    2. Assume that thread_A is not completed and thread_A.isAlive() condition is passed, then starting of thread_B before thread_C is not always guaranteed by Java thread scheduler. Again your application fails.
    3. Assume that thread_B starts before thread_C and if thread_B completes before thread_B.isAlive() is checked then the if condition fails and thread_D is never started. Again your application fails.

    Now a point to ponder:
    There is no need to check if the thread is alive after its join() method is called. It is an unnecessary runtime overhead.

    EDIT
    OK, Here is the modified version of code..I hope it would let you understand the dynamics of thread:

    class RobotController implements Runnable
    {
        private final Object lock = new Object();
        private void notifyThread()
        {
            synchronized(lock)
            {
                lock.notify();
            }
        }
        public void run() 
        {
            synchronized(lock)
            {
                try
                {
                    System.out.println(Thread.currentThread().getName() + " started");
                    lock.wait();
                    System.out.println(Thread.currentThread().getName()+ " stopped");
                }
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    
        public static void main(String args[]) throws InterruptedException
        {
            RobotController rca = new RobotController();
            RobotController rcb = new RobotController();
            RobotController rcc = new RobotController();
            RobotController rcd = new RobotController();
    
    
            Thread thread_A = new Thread(rca,"Thread A");
            Thread thread_B = new Thread(rcb,"Thread B");
            Thread thread_C = new Thread(rcc,"Thread C");
            Thread thread_D = new Thread(rcd,"Thread D");
    
            thread_A.start();
            while (thread_A.getState() != Thread.State.WAITING)
            {
                Thread.sleep(100);
            }
            thread_B.start();
            thread_C.start();
            while (thread_B.getState() != Thread.State.WAITING && thread_C.getState() != Thread.State.WAITING)
            {
                Thread.sleep(100);
            }
            thread_D.start();
            while (thread_D.getState() != Thread.State.WAITING)
            {
                Thread.sleep(100);
            }
            rcd.notifyThread();
            thread_D.join();
            rcc.notifyThread();
            thread_C.join();
            rcb.notifyThread();
            thread_B.join();
            rca.notifyThread();
        }
    
    }
    

    And here is the output:

    Thread A started
    Thread B started
    Thread C started
    Thread D started
    Thread D stopped
    Thread C stopped
    Thread B stopped
    Thread A stopped
    

提交回复
热议问题