0

I a have project that I'm working on dealing with multithreading and am struggling a bit to understand how a goal like this could be accomplished.

The program represents a kitchen, and I have an oven which I want to run for an X amount of time, I want the thread to block for X amount of time, and at the end of the block, return whether the dish was fully cooked (was the oven turned on the entire X amount of time?)

The issue I am having is that when my thread blocks, my boolean could be flipped off and back on, and I have no way to capture that, just the ending result.

Is there something in Java that I could use that would allow me to block on the thread until either the time has elapsed or the boolean changes?

My current code is as follows:

public boolean cookFor(int minutes)
{
    //Milliseconds of block should be relative to temperature
    int milliseconds  = (minutes * 100);
    try
    {
        //Block thread for milliseconds time
        Thread.sleep(milliseconds);
    }
    catch (InterruptedException e)
    {
    }
    //Status indicates if Oven is On or Off
    return status;
}
JamesRichardson
  • 131
  • 2
  • 13
  • 1
    Are you familiar with `wait/notify`? – Kayaman Feb 07 '18 at 21:21
  • Are you suggesting to wait(milliseconds) and if another thread changes the boolean value have it notify that thread? How will I know if the thread has woken up from the wait being finished or from being notified? – JamesRichardson Feb 07 '18 at 21:26
  • That's exactly what I'm suggesting, very good! But do you really need to know which one ended the wait? Of course if `status` is still `false`, you'll know for sure that the `wait()` timed out. If it's `true`, then it's likely that it was notified (although not guaranteed, it could time out and then get set to `true` if timing is proper). – Kayaman Feb 07 '18 at 21:29
  • Ah that makes a lot of sense! The issue I'm running into now when I wait(milliseconds) and then notifyAll() if the oven is shut off is that I get an IllegalMonitorStateException, I've been trying to read the Java API to understand why this occurs, but when I create a main() and an instance of the object and call oven.off() I get the error, but I only have a single thread so far which is confusing me... – JamesRichardson Feb 07 '18 at 21:36
  • When calling `wait()` or `notify()` you need to own the object's monitor, which means you need to have synchronized on the object (so `synchronized(foo) { foo.wait(); }` works). You also need more than one thread for the code to work, otherwise it just won't make sense. – Kayaman Feb 07 '18 at 21:39
  • There are plenty of other examples on using wait/notify besides the duplicate, so you should be able to find a lot of examples with searching. It's definitely the right way to go with this problem. – Kayaman Feb 07 '18 at 21:50
  • I've got it almost working except whenever I notifyAll() it never causes my Wait() to throw an exception, even when I use a timer to force it to happen before the wait finishes. Could this be due to the synchronized()? – JamesRichardson Feb 07 '18 at 21:51
  • Notify doesn't cause an exception to the thread in `wait()`, it just wakes it up. `Thread.interrupt()` would cause an exception, as it's an exceptional circumstance. – Kayaman Feb 07 '18 at 21:52
  • Oh that was a misconception I had, then I seem to still have the problem where I can't see if the thread ended it's full wait time or if the oven status changed early. – JamesRichardson Feb 07 '18 at 21:53
  • See my second comment. – Kayaman Feb 07 '18 at 21:55

0 Answers0