Consider the following algorithm to sort an array of strings and remove duplicates, using two threads.
The first thread keeps carrying out the following operation: Starting at the beginning of the array, look for two adjacent elements that are out of order. If such a pair is found, swap the elements.
The second thread keeps carrying out the following operation: Starting at the beginning of the array, look for two adjacent elements that are equal. If such a pair is found, remove one of the elements.
This is a very inefficient algorithm, but it will eventually sort the array and remove all duplicates.
However, the program below never stops because both threads run forever.
Your task is to find a way to terminate each thread when it cannot do any further work.