5

How can I make a thread wait without releasing the lock ? If this is not possible, then how can I pause a thread while a certain condition is not met and unpause it as soon the condition is met or when I notify it

bvdb
  • 22,839
  • 10
  • 110
  • 123
Boyar
  • 53
  • 2
  • 1
    Possible duplicate of [How do I make a delay in Java?](https://stackoverflow.com/questions/24104313/how-do-i-make-a-delay-in-java) –  Mar 26 '19 at 10:31
  • 1
    I'm thing `Object#wait` and `Object#notify` or a `ReentrantLock` and `Condition` – MadProgrammer Mar 26 '19 at 10:31
  • Maybe try using a Countdown latch - https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html – fatcook Mar 26 '19 at 10:34
  • As @MadProgrammer mentioned, ReentrantLock and Condition may be a good target. Tell us more - can you elaborate on your use case? – Highbrainer Mar 26 '19 at 10:53
  • Java 5, which was released in 2004, added package `java.util.concurrent`. This package contains classes and interfaces that comprise the _Java Concurrency Utilities_. There have been additions to the concurrency utilities in later Java releases but the majority of them are the same in Java 12, which was released a week ago, as they are in Java 5. There are many online resources as well as books that can help you master these utilities. Just do an Internet search for "java util concurrent". – Abra Mar 26 '19 at 10:58
  • @Highbrainer Basically I have a array box with 2 slots I have 2 threads, one for inserting numbers in the array and one for removing The one that inserts numbers is looped 6 times while the remove is just one I have set it if the box is full for the insert threads to wait but that releases the lock which results in many insert threads entering and going to sleep and when the remove thread notifies after it clears a slot from the box, all the waiting threads attempt to insert a number in the one slot. – Boyar Mar 26 '19 at 11:12
  • @Boyar I see. Then maybe you should have a look at the `ArrayBlockingQueue` https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html. Also it reminds me of the example they give in the javadoc for https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Condition.html : it's very close to what you describe... HTH! – Highbrainer Mar 26 '19 at 13:08

1 Answers1

0

Concept of wait/notify

Waiting/Notifying can only be done in the scope of a lock (e.g. a synchronized method or synchronized block).

Both the wait and notify should synchronize on the same object.

The waiting thread:

synchronized(lockObject) {
  lockObject.wait(); <-- it will wait here until the notify is called.
}

The notifying thread:

synchronized(lockObject) {
  lockObject.notify();
}

The lockObject can be anything. It could just be a new Object(), but very often it could be some collection, a logical object that represents a printer, ... anything.

Hint: Judging from the comments section, you may need the following: The notify releases just one waiting thread. But you could actually have multiple threads waiting. If you want to release all of them, use notifyAll.

Same thing, but with synchronized methods

Alternatively, you could create a class, and use method level locking.

class LockObject {
  public synchronized void waitMethod() {
    wait();
  }

  public synchronized void notifyMethod() {
    notify();
  }
}

Again both have to use the same object to lock on.

LockObject instance = new LockObject();

Then the waiting thread:

instance.waitMethod();

And the notifying thread:

instance.notifyMethod();

If you are serious about this

Also take a look inside the concurrency packages and tutorials of the JDK, and you will find more advanced locking objects. Just to name one, countdown latches are powerful.

bvdb
  • 22,839
  • 10
  • 110
  • 123