The code flow is as follows:
The main method creates 2 threads, setting their type (via the ExampleThread constructor) to 0 and 1.
When the thread starts it checks for its type, if it's 0 - it tells the CountDownLatch object to decrement it's count field and acquires the semaphore. After the run() method execution is finished, the semaphore is released.
If the thread type is 1 - it checks the latch object's count field. If it equals to 0 - the thread execution continues, otherwise - it waits for it to become 0. Obviously, in order to acquire the semaphore, it should first be released by the 0 type thread.
Consequently, starting the code would have the 0 type thread print the numbers from 0 to 4, and the 1 type thread wait for it to finish. After the 0 type thread has released both the latch and the semaphore, the 1 type resumes and prints the letters array contents.
Войдите чтобы поставить Нравится
Войдите чтобы прокомментировать