Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

deadlock??

Thread view: 
tcl - 15 Dec 2006 02:42 GMT
The following is not my code, but I have to maintain it.
The two classes have been simplified to get to the point,
try-catch blocks, among other things,  are omitted.
MyStuff has a main thread run().  And clients call
addNotification() at times.
Calling the addNotification() method in a myStuff object
sometimes hangs indefinitely.
Questions:
- should the calls mySem.release()
 and mySem.acquire() be enclosed inside the synchronized blocks?
- can anyone tell me what could cause the deadlock or hanging?
Thanks a million,
ted

class semaphore
{
  private long myCount = 0;
  public semaphore() {myCount = 0;}
  public void acquire()
  {
     synchronized(this)
     {
        while(myCount == 0){ wait(); }
        myCount--;
     }
  }
  public synchronized void release()
  {
     myCount++;
     notify();
  }
}

class myStuff
{
   private LinkList myList=null;
   private semaphore mySem = null;
   public myStuff()
   {
      myList = new LinkList();
      mySem = new semaphore()
   }
   public void addNotification(xyz a)
   {
      sychronized(myList)
      {
         myList.addList(a);
      }
      mySem.release();
   }
   public void run()
   {
       while(myThreadRun)
       {
          mySem.acquire();
          xyz ref=null;
          synchronized(myList)
          {
             ref = myList.removeFirst();
          }
          ref.foo();
       }
   }
hiwa - 15 Dec 2006 03:15 GMT
> The following is not my code, but I have to maintain it.
> The two classes have been simplified to get to the point,
[quoted text clipped - 59 lines]
>         }
>     }
Post a small demo code that is generally compilable, runnable and could
reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html
Thomas Hawtin - 15 Dec 2006 14:38 GMT
> class semaphore

It would be a good deal less confusing if you used initial caps for
class names.

There already is a Semaphore class in the Java library. Why not use
that? If you are using a really old version of Java, then there is the
JSR166 backport.

> {
>    private long myCount = 0;
>    public semaphore() {myCount = 0;}

Why assign myCount twice. In fact, why assign it at all. Without the
assignment, you could get away with unsafe publishing.

>    public void acquire()
>    {
[quoted text clipped - 14 lines]
> {
>     private LinkList myList=null;

The Java library already has a linked list class.

>     private semaphore mySem = null;
>     public myStuff()
>     {
>        myList = new LinkList();
>        mySem = new semaphore()

Again why assign twice? Marking the fields final would be a good idea.

>     }
>     public void addNotification(xyz a)
[quoted text clipped - 11 lines]
>            mySem.acquire();
>            xyz ref=null;

No need to assign ref here. In fact, it could be made final.

>            synchronized(myList)
>            {
[quoted text clipped - 3 lines]
>         }
>     }

At no point in this code to acquire either the semaphore or LinkList
intrinsic lock at the same time, so that isn't deadlocking. Does jstack,
ctrl-\ (ctrl-break on Windows) or your debugger give any clues?

There isn't any need to move the acquire into the synchronized block.

I suggest using the much better BlockingQueue implementations supplied
in the Java library (and JSR166 backport).

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.