|
|
Subject:
Java 1.5 Threads Question: Deadlock occuring when synchronizing from static mthd
Category: Computers > Programming Asked by: davidparks21-ga List Price: $5.00 |
Posted:
26 May 2005 17:49 PDT
Expires: 25 Jun 2005 17:49 PDT Question ID: 526106 |
Hi there. I am using Java 1.5 and have a question about threads. I have 5 threads running which all lock on a static object called syncobject to do their work. example: static syncobject=new Object(); static boolean shutdown = false; public void run(){ synchronized(syncobject){ //Check if shutdown set to true //Exit if it is, otherwise do work. //When there is no work to do. syncobject.wait() } } When I want to shut down all threads I call a static method which sets a boolean variable to indicate shutdown. In that static method I need to notify all threads waiting on syncobject. I would think I would do this: static void callShutdown(){ shutdown = true; synchronized(syncobject){ syncobject.notifyAll() } } I expect this would interrupt all of the threads, as soon as I exit the synchronized block they would all go into their loop, see the shutdown variable set and exit accordingly. Instead what happens is that all threads are blocked in the run method at the synchronized block. When I change the static method to not synchronize notifyAll() the threads drop into the synchronized block correctly (but then of course my notifyAll() fails later on since it's not synchronized). Any thoughts? Thanks, David |
|
There is no answer at this time. |
|
Subject:
Re: Java 1.5 Threads Question: Deadlock occuring when synchronizing from static mthd
From: jtolmach-ga on 03 Jun 2005 21:32 PDT |
Do you really need to synchronize your run method? Doesn't this defeat the purpose of using multiple threads? I suggest moving away from that constraint if you can. If you are just trying to develop a method to shut down all running threads then what you are doing will work. I recommend this book - "Concurrent Programming in Java" http://gee.cs.oswego.edu/dl/cpj/ |
Subject:
Re: Java 1.5 Threads Question: Deadlock occuring when synchronizing from static
From: bugloaf-ga on 30 Jun 2005 11:38 PDT |
I don't know if this is the source of your problem but this static void callShutdown(){ shutdown = true; synchronized(syncobject){ syncobject.notifyAll() } } Should be written as this static void callShutdown(){ synchronized(syncobject){ shutdown = true; syncobject.notifyAll() } } Otherwise you could have a race condition, which could result in a deadlock. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |