• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets, threading, and concurrency  RSS feed

 
Alex Belt
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that accesses external resources. I have a class that stores references which indicate which of these external resources are in use. I want the servlet to block temporarily if the resource it needs is listed in the reference store. I have tried Thread.currentThread.wait(500L), but I get an 'IllegalMonitorStateException: current thread not owner' thrown. I cannot synchronize directly on the resources because I have multiple threads accessing the resources, and I cannot afford the performance reduction associated with the SingleModelThread interface. Is there another method I have overlooked that will cause the servlet thread to wait? One last thing: I am limited to the Servlet 2.0 and Java 1.1.8 specs because of the software involved.
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Alex, let's take this little-by-little...
You do:
Thread.currentThread().wait(500L);
Be extra careful here: There is no method wait() declared in class java.lang.Thread; methods wait() and notify() are a nice little pair that is declared all the way up the class hierarchy at the java.lang.Object class. Say you have a variable o which is pointing to some object (it may be an Object, a Thread, a JComboBox, a Foo, whatever):
Object o = ...;
o.wait();
What is the above code really doing? Well, it "Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object" (I am quoting the API here). In threading lingo, o acts as the monitor facilitating the communication between two threads: The thread that executes the o.wait() line of code will wait (i.e. block) there, until some other thread executes an o.notify() (or o1.notify() or myObject.notify(), whatever the reference to the monitor is named in that other thread) which is in effect telling to the original thread (the one that executed the wait()): "Please proceed; I am releasing you from your self-imposed prison".
Now, here is why are you getting an IllegalMonitorStateException in your code: As the API for the wait() method says:
"Throws:
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor."
Note that you are using the Thread.currentThread() object as your monitor. Apparently, the current thread is NOT the owner of that object! For a thread to become the owner of a monitor, the thread should execute a synchronized block on that monitor. For example:
synchronized (o) {
o.wait();
}
Hope this helps,
Panagiotis.
[ August 05, 2002: Message edited by: Panagiotis Varlagas ]
 
Alex Belt
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually got the thing figured out. Changed it to Thread.sleep(500L) and it works like a champ. Can't believe I overlooked that. Thanks Panagiotis, I'll keep that inmind in the future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!