As in Java run method in a separate thread?

Good time, there is a need during program execution (main thread) to execute the methods of another class, but only when it will set a flag.
Came to the decision that it is possible in the main thread to create a method which would create a thread that would be a method that waits (using while) set the required flag.

Please tell me, was it the only solution? Maybe there's something without the while loop? ))
June 7th 19 at 14:55
2 answers
June 7th 19 at 14:57
Solution
It's a strange decision too gromozdok and unreliable. Usually this is solved by installing a listener. And the class in which the flag notifies this listener.
June 7th 19 at 14:59
You can use CountDownLatch or a CyclicBarrier. At the docks there are examples.
Thank you for looking. It's not really my case. I need to not have any locks. I add new functionality, and this should be done so that it is neither on what does not influence )) Just want to say Class1 class, create a method myWait () which would expect/check the status field of another class Class2 as long as it does not change, but at the same time, other methods in Class1 after myWait() would run as before, not waited for the completion of myWait(). - taya.Christiansen commented on June 7th 19 at 15:02

Find more questions by tags Javamultithreading