0

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!

Kayaman
  • 72,141
  • 5
  • 83
  • 121
user2973438
  • 311
  • 2
  • 6
  • 9
  • Are you sure you have 100 `for` loops in thread 1, or is that just exaggerating for effect? Assuming you're writing something reasonably sane, the approach you've outlined in your question seems perfectly sensible. If it's a nested loop, check for pause in the outer loop. – Robert Harvey Nov 12 '13 at 17:27
  • ok sorry that is a bit of an exaggeration, but I really do have a lot of loops and if statements.. and nested loops and nested statements in Thread1's run() – user2973438 Nov 12 '13 at 17:29
  • http://stackoverflow.com/questions/11989589 – Robert Harvey Nov 12 '13 at 17:30
  • Basically I would like an infinite number of checkpoints "wait()" in Thread1's run() but that is not practical.... – user2973438 Nov 12 '13 at 17:32
  • In a kaveri APU, you may be able to use Thread1 as opencl(jocl/aparapi) kernel of "integrated gpu" and control myLock from CPU-cores(conventional threads). You may need to pass myLock as a kernel-parameter to have same address though. – huseyin tugrul buyukisik Nov 12 '13 at 19:00
  • Why exactly do you want to pause the Thread? Because I am sure this is more like an architectural issue. – TwoThe Nov 13 '13 at 13:57

1 Answers1

0

What you want is the ability to suspend a thread at any moment, at the command of another thread. That ability has been deprecated for a long time now, due to its unsafety.

So yes, you actually do have to manually insert checkpoints into your code where it is safe for the thread to enter the waiting state. Multithreading is a rough terrain and there's no royal way about it.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436