7 Replies Latest reply: Nov 19, 2007 10:20 PM by 807603 RSS

    IllegalMonitorStateException from StringBuffer.append()

    807603
      Hi,

      I am having a not-easily reproducible problem about IllegalMonitorStateException.
      I have an event dispatching Thread that runs fine for roughly 10 hours. But after that "IllegalMonitorStateException" happens
      most of the time from JRE methods like StringBuffer.append() or Hashtable.clear().

      One observation that i had made is that the IllegalMonitorStateException is happening from synchronized methods.
      I have never received this IllegalMonitorStateException from any wait() ot notify() calls in my code.

      Could somebody please help me with this.
      I am using pbp 1.0

      Thanks in advance.
        • 1. Re: IllegalMonitorStateException from StringBuffer.append()
          796365
          The description of the error is pretty definitive:

          Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.

          Try adding some descriptive println statements to the program when a thread attempts either action specified, and print the relative information to a log for review.
          • 2. Re: IllegalMonitorStateException from StringBuffer.append()
            807603
            Thanks "ChuckBing" for the reply
            ChuckBing wrote:
            Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
            I understand that IllegalMonitorStateException is thrown normally when we try to do a wait(), notify() or notifyAll() while not holding lock on the same object.

            But here the scenario is different. See the stack trace log.
            ---------------------------------
            java.lang.IllegalMonitorStateException: current thread not owner
                 at java.lang.StringBuffer.append(I)Ljava/lang/StringBuffer;(Unknown Source)
            .....................
            .....................
                 at java.lang.Thread.startup(Z)V(Unknown Source)
            ---------------------------------

            The exception came from append() method of StringBuffer class. I went through the source code of the StringBuffer class and found the append() method's implementation as

            -------------------------------------------------------------------
            public synchronized StringBuffer append(String str) {
                 if (str == null) {
                 str = String.valueOf(str);
                 }

                 int len = str.length();
                 int newcount = count + len;
                 if (newcount > value.length)
                 expandCapacity(newcount);
                 str.getChars(0, len, value, count);
                 count = newcount;
                 return this;
            }
            -----------------------------------------------------------------------

            So the situation is that an IllegalMonitorStateExceptiion is being thrown from a synchronized method where there are no wait(), notify() or notifyAll() being used.

            Other than wait(), notify() or notifyAll(), what other Java programming mistake can cause such an Exception to be thrown?
            • 3. Re: IllegalMonitorStateException from StringBuffer.append()
              800308
              Other than wait(), notify() or notifyAll(), what causes an IllegalMonitorStateException?
              Synchronized methods.

              Remember what synchronized means?

              http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html

              And how do you supplose it does what it does? I guess a synchronized method:
              * on entry to method/block - lock this or wait()
              * on exit from method/block - notifyAll()

              Edited by: corlettk on Nov 10, 2007 8:14 AM
              • 4. Re: IllegalMonitorStateException from StringBuffer.append()
                807603
                Thanks for the reply "corlettk".
                but
                I couldn't understand what you have said.

                I went though the link you had given; but found nothing new.

                Please help me to rectify this issue.
                • 5. Re: IllegalMonitorStateException from StringBuffer.append()
                  800308
                  The point is that synchronized means that the method implicitly calls wait() and notifyAll()... so it's same as tracking down any other thread contention issue... ie: notoriously difficult.

                  Your observation that this problem occurs after 10 hours is interesting. I presume this is serverside application... Is the service booted at the same time everyday? and does that that mean that problem occurs at roughly the same time of every day? What's happening around that time?

                  I suggest you search the http://java.sun.com/docs/books/jls/ for "synchronized".
                  • 6. Re: IllegalMonitorStateException from StringBuffer.append()
                    807603
                    corlettk wrote:
                    The point is that synchronized means that the method implicitly calls wait() and notifyAll()
                    As you have said, since for synchronized methods, wait() and notifyAll() are done implicitly by jvm, is it JVM who is responsible if something goes weird about synchronized methods?

                    Can we suspect this as a JVM bug?

                    Again this problem occurs on long run; roughly after 10 hours.Not at any particular time of day
                    And my application is not a Server side one. Its running on an embedded system [ PBP 1.0]
                    • 7. Re: IllegalMonitorStateException from StringBuffer.append()
                      807603
                      Somebody please help me to fix this problem :((