I have two threads in two different classes, Thread1 and Thread2.
In Thread 1 I have something like this:
public class Thread1
{
public static boolean pause = false;
public void run()
{
while(true)
{
synchronized (myLock)
{
for (int i=0; i<5; i++)
{
//a for loop
}
if (someCondition1)
{
//an if statement
}
while (someCondition2)
{
//a while loop
}
}
}
}
}
In Thread 2 I have something like this:
public void run()
{
Thread1.pause=true;
synchronized(myLock)
{
//do some mutually exclusive task while Thread1 waits
}
Thread1.pause=false;
myLock.notify();
}
}
Of course, Thread1.start(); and Thread2.start(); happen elsewhere in another program, but assume both threads are started.
The problem is I don't know where to put my wait() in Thread 1.
What I want: Thread 2 to be able to interrupt Thread1 regardless of where I am in Thread1. If I have in total 100 for loops, while loops and if statements in Thread1's run() method, I DON'T want to put a checkpoint
if (paused)
{
wait();
}
inside every one of those loops in Thread1. Is there a way to pause Thread1's run() method regardless of which loop/if statement I am in? (i.e. regardless of where I am currently in Thread1?)
Thanks!