Wait for all threads, then sort and output