0

Is there a way in Java to run 3 functions concurrently but execute another function only when the previous three have all been completed.

For example:

public void methodOne(){
            for(int i = 1; i <= 10; i++)
        {
            System.out.println(i);
        }
            
        }
        
        public void methodTwo(){
            for(int i = 1; i <= 100; i++)
        {
            System.out.println(i);
        }
            
        }
        
        public void methodThree(){
            for(int i = 1; i <= 10; i++)
        {
            System.out.println(i);
        }
            
        }
        public void methodCompleted (){
  

           System.out.println ("All functions completed");

       }






The idea is to have methodOne, methodTwoand methodThree run concurrently but when all three are done then methodCompleted runs.

Nicole Foster
  • 381
  • 4
  • 14
  • 1
    If you want a self made solution then you could make a synchronized integer flag for example that is incremented by 1 each time one of the three methods finishes, and if the incremented value is 3 then call `methodCompleted()` because all three methods will have finished. If you do it this way then the increment method should be synchronized, and return the increment amount so that the threads don't overlap if they finish at the same time. – sorifiend Aug 05 '22 at 03:56
  • I will check out. – Nicole Foster Aug 05 '22 at 03:58

0 Answers0