Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 472127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:03:19+00:00 2026-05-13T00:03:19+00:00

I want to have such kind of work flow using explicit lock/condition variables (It’s

  • 0

I want to have such kind of work flow using explicit lock/condition variables (It’s a course project which mandates this style.): A is the main class, it asks B to do some job from time to time. B has a worker class C which constantly queries B about new jobs to do and do it. After C finishes, it will call A’s callback function to notify A the job is done.

However when I try to run the program, I get an IllegalMonitorStateException, when the callback() is trying to notify the doit() function.

exception in thread "Thread-0" java.lang.IllegalMonitorStateException
        at java.lang.Object.notifyAll(Native Method)
        at Test$A.callback(Test.java:49)
        at Test$C.run(Test.java:115)

I looked at the javadoc and some Q&A about this exception, but still no idea why I get this.

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;

public class Test {
    public class A
    {
        private ReentrantLock lock;
        private Condition cond;
        private boolean bool;
        private B b;
        public A()
        {
            this.lock = new ReentrantLock();
            this.cond = lock.newCondition();
            b = new B(this);
            bool = false;
        }

        public void doit()
        {
            try {
                lock.lock();
                b.letgo();
                while (!bool) {
                    System.out.println("A::doit() Block.");
                    cond.awaitUninterruptibly();
                }
                System.out.println("A::doit() Done.");
            }
            finally {
                lock.unlock();
            }
        }

        public void callback() {
            try {
                lock.lock();
                bool = true;
                cond.notify();
                System.out.println("A::callback() done.");
            }
            finally {
                lock.unlock();
            }
        }
    }

    public class B
    {
        private C c;
        private ReentrantLock lock;
        private Condition cond;
        private boolean bool;
        public B(A a)
        {
            this.lock = new ReentrantLock();
            this.cond = lock.newCondition();
            bool = false;
            c = new C(a, this);
            c.start();
        }

        public void letgo()
        {
            try {
                lock.lock();
                bool = true;
            }
            finally {
                lock.unlock();
            }
        }

        public void get()
        {
            try {
                lock.lock();
                while (!bool) {
                    cond.awaitUninterruptibly();
                }
                bool = false;
                return;
            }
            finally {
                lock.unlock();
            }
        }
    }

    public class C extends Thread
    {
        private A a;
        private B b;

        public C(A a, B b)
        {
            this.a = a;
            this.b = b;
        }

        public void run()
        {
            while (true) {
                b.get();
                a.callback();
            }
        }
    }

    public static void main(String args[])
    {
        Test t = new Test();
        t.test1();
    }

    public void test1()
    {
        A a = new A();
        a.doit();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:03:19+00:00Added an answer on May 13, 2026 at 12:03 am

    Use the signal() method on Condition in place of notify().

    While you can successfully synchronize on a Condition instance, and then use the traditional wait() and notify() methods, you might as well just use an Object if you aren’t using the extended capabilities of the concurrent classes.

    Condition was intended to be used with the equivalent methods await() and signal(), and their enhanced variants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such view that handles user registration. After creating new user i want
I am new to php. I want to have a form such as this:
I want to have a generic function for a few types such as long,
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Let's say I have a drive such as C:\ , and I want to
I have randomly created strings such as H*P2[-%-3:5RW0j*;k52vedsSQ5{)ROkb]P/*DZTr*-UX4sp What I want to do is
want to have a Hyperlink-Button in a gridView in which I can display a
I want to have multiple objects share a reference through a private field, such
I have some beans for which, in specific injections, I want to add a
I have several strings which look like the following: <some_text> TAG[<some_text>@11.22.33.44] <some_text> I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.