I need to have two classes, one class has two methods each of which will take a while to execute completely. The second class is given information which will decide which one of the two methods in the first class will execute. It is important however that if the second class (which will be executing at the same time in its own thread) decides that the other of the two methods should execute, the first class would go straight to executing the other method and not have to wait for the first to finish.
If class A has the two methods and class B is in parallel in a different thread deciding which method to execute then here is an example:
A is executing method 1 which will take a while to return. During the execution of method 1, class B decides method 2 should execute which means class A should immediately stop doing method 1 and go on to execute method 2.
Is this possible? Maybe with interrupts?
thanks
Here’s an outline of something that should work:
Note:
InterruptedExceptioncan happen beyond your control, so you should check it’s due to an explicit switch,volatileon theswitchToOtherfield used from several threads,InterruptedException