When you use threads you should have a decrement at the end of the thread, and so you can wait until that variable hits 0 before starting the vpn process using a loop while that number is greater than 0 and waiting in the loop while. I explain all about that and more here, you can skip to 18:50 in for just the multithreading part if you want to but watching from the beginning will help with knowing whats going on in the code: