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

  • SEARCH
  • Home
  • 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 8257553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:14:09+00:00 2026-06-08T02:14:09+00:00

System.out.println(Thread state: + threads[i].getState()); threads[i].notify(); Produces the following output: Thread state: WAITING Exception in

  • 0
System.out.println("Thread state: " + threads[i].getState());
threads[i].notify();

Produces the following output:

Thread state: WAITING
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at MyPakc.An.run(An.java:49)
at java.lang.Thread.run(Thread.java:679)

What is going on? Why can I not notify a sleeping thread?

EDIT: The code for the threads[] class:

package Part2;

import java.util.List;
import javax.swing.JPanel;



class BThread extends Thread{
    private boolean completedThisIter = false;

    @Override
    public synchronized void run() {
        while (true) {
            completedThisIter = false;
            doStuff()
            System.out.println("Completed iter");
            completedThisIter = true;
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean getCompletedThisIter() {
        return completedThisIter;
    }
}

EDIT: Here is the code that calls this

public synchronized void run(){
// OTHER STUFF
    for (int iter = 0; iter < 1212; ++iter){
        System.out.println("Iter " + iter);
        lastAssignedBallIndex = -1;
        for (int i = 0; i < numThreads; i++) {
            //System.out.println("Num " + numThreads + "  " + i);
            //ballThreads[i] = new BallThread(ballList.subList(lastAssignedBallIndex+1,lastAssignedBallIndex+numBallsPerThread),
            //        ballPanel);
            //lastAssignedBallIndex += numBallsPerThread;
            System.out.println("State " + ballThreads[i].getState());
            if (ballThreads[i].getState() == Thread.State.NEW) {
                ballThreads[i].start();
            } else { //if (ballThreads[i].getState() == Thread.State.BLOCKED) {
                System.out.println("Thread state: " + ballThreads[i].getState());
                ballThreads[i].notify();
            }
        }
        //try{
            for (int i = 0; i < numThreads; i++) {
                while (!ballThreads[i].getCompletedThisIter()) {
                    System.out.println("iter:" + iter + " ball:" + i + "  " + ballThreads[i].getCompletedThisIter());
                    //wait(); // TODO elliminate polling here
                }
            }
            System.out.println("Joined");
        //}
       // catch(InterruptedException ie){ie.printStackTrace();}


        ballPanel.repaint();
        notifyAll();
        try{
            Thread.sleep(2);
        }
        catch (InterruptedException ie){}
    }
}
  • 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-06-08T02:14:11+00:00Added an answer on June 8, 2026 at 2:14 am

    You’re printing out the state of a ballThreads[i] then notifying a threads[i]. Not sure if this is intended behavior but you’re not allowed to notify a thread when you don’t own the object’s monitor. Are you sure you’re calling this inside a synchronized() block for the threads[i] object?


    EDIT:

    Yes, the method that this code is taken out of is synchronized

    After your edit to your question, the synchronized is on the method, and not the monitor of the object, you need to put your code in a block that looks like this:

    synchronized(threads[i]) {
        // some stuff
        threads[i].notify();
    }
    

    The important bit here (as opposed to the synchronized keyword in a method declaration) is that you synchronize on an Object, then inside this block, you call notify() on the Object. Examples:

    public void run()
    {
        synchronized(myObject) {
            // do some stuff
            myObject.notify();
        }
    }
    

    or

    public void run()
    {
        synchronized(thread1) {
            // do some stuff
            thread1.notify();
        }
    }
    

    or

    public void run()
    {
        synchronized(syncObject) {
            // do some stuff
            syncObject.notify();
        }
    }
    

    See the pattern? More info here: http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

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

Sidebar

Related Questions

public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
System.out.println(1+2+3); System.out.println(1+2+3); output:- 33 123 First case is understood but the second case is
This code: System.out.println(String.format(%f, Math.PI)); System.out.println(Math.PI); produces that result on my computer: 3,141593 3.141592653589793 Why
Why does System.out.println(-1<<32) display -1 in Java? Is there any root cause? I hope
class Process implements Runnable{ private ThreadProcessing MIDlet; public Process(ThreadProcessing MIDlet){ this.MIDlet = MIDlet; System.out.println(Thread
System.out.println( Arrays.deepToString( abc<def>ghi.split((?:<)|(?:>)) ) ); This prints [abc, def, ghi] , as if I
System.out.println(matcher.group(1)); System.out.println(matcher.group()); I like to know what's the difference between the above two codes.
System.out.println(ganzeZeile[26]); System.out.println(filter.get(11)); System.out.println(ganzeZeile[26].contains(filter.get(11))); ganzeZeile is an array of Strings. filter is an ArrayList of
class A { void F() { System.out.println(a); }} class B extends A { void
for (String msg : messages) { System.out.println(msg); } Say that messages is a properly

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.