1

I am new to multithreading and I am attempting to implement a unbounded queue. I know that notifyAll wakes up all the threads that are waiting on the object's monitor. But what happens if I return object immediately after waking up the threads.

Will this cause issues, i.e T2 has the lock and is in the synchronized block whilst T1 (original thread) is returning the object after releasing the lock? As far as I understand, because the state is not being changed, it should be okay.

The code is as following:

 public <T> T remove(SomeObj someObj) throws InterruptedException {
    synchronized (obj) {
        while(queue.isEmpty()) {
            obj.wait();
        }
            //some calculation here
        T foo = someObj.getTValue();
        queue.remove(foo);
        return foo;
    }
}

public <T> void add(T foo) {
    synchronized (obj) {
        queue.add(foo);
        obj.notifyAll();
    }
}
Tima M
  • 11
  • 2
  • `monitor.notify();` - Are you sure? – akuzminykh Jul 13 '20 at 20:52
  • oops, I meant monitor.notifyAll() – Tima M Jul 13 '20 at 21:11
  • Are you still sure? ;) (Hint: [`IllegalMonitorStateException`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/IllegalMonitorStateException.html)) – akuzminykh Jul 13 '20 at 21:13
  • And the name of your method is `add` but it removes from the queue? – akuzminykh Jul 13 '20 at 21:26
  • @DavidSchwartz There is another method that uses the same synchronized block and lock to add something to the same queue (it is actually a arraylist data structure). T2 needs to wait for T1 to remove an element from the list before T2 can add an element to the list to ensure thread safety. I have updated the code – Tima M Jul 13 '20 at 21:39
  • @DavidSchwartz ah I see, so if I had understood correctly, I can remove notifyAll() from remove method? – Tima M Jul 13 '20 at 22:03
  • @TimaM Correct. You only need to call `notify` or `notifyAll` when you've done something that another thread might be `wait`ing for. – David Schwartz Jul 13 '20 at 22:24
  • 1
    A blocking queue is bounded - that's how/why it blocks. If your queue is unbounded, how can it be blocking? – Bohemian Jul 13 '20 at 22:43
  • @Bohemian If the queue is empty and a thread tries to read from it. – David Schwartz Jul 13 '20 at 23:14
  • 1
    Perhaps, you should spend some more time thinking about what you want to ask and write code matching the question, instead of posting half-baked code and edit it beyond all recognition. I still can’t see your scenario (code that will “return object immediately after waking up the threads”) in your code. Regardless of the code example, in what regard do you think, are returning an object and notifying threads connected at all? – Holger Jul 14 '20 at 16:58
  • @Holger - I think this is how you learn, by trying and failing and if it doesn't make sense then asking questions... David Schwartz helped me see where my confusion was and now I have a working implementation, so I'm not sure what the issue is? I have followed the rules here by showing my attempt and understanding before posting a question - is the problem that I edited question? – Tima M Jul 15 '20 at 09:35
  • 2
    The purpose of this website is to collect questions and answers, so people can search whether someone already had the same question and found solutions. It’s great that you have a working solution, but future readers still are not even able to recognize what your actual question was, not to speak of recognizing the solution to your problem. It has nothing to do with learning, when people have to tell you that you used nonexistent variable names or mixed up adding and removing methods and such alike. Such mistakes could have been avoided in the first place, without wasting other people’s time. – Holger Jul 15 '20 at 10:08
  • Okay, point noted. As I am sure, you're aware I am completely new here so I will do as you advised above. I think if you tell someone to do something as a constructive criticism, it is received well. – Tima M Jul 15 '20 at 10:24

0 Answers0