• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Blocked call to Semaphore.acquire produces null InterruptedException message.

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This SSCCE creates a Semaphore with one permit, acquires that permit, then starts a thread that also tries to acquire that permit. The new thread therefore blocks until the main thread interrupts it, which deblocks the second thread and causes the second thread's call to acquire to throw an InterruptedException.

Calling the exception object's getMessage method returns a null. If the blocking call to acquire is replaced with a call the Thread.sleep, the exception object's getMessage method returns a string ("sleep interrupted").

Why does the exception object's getMessage method return a null if the interruption occurred while the thread was blocked in a call to acquire?

 
Marshal
Posts: 4010
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Semaphore#acquire() calls a method which calls another method which can throw new InterruptedException(). No arguments means no message which will result in Throwable returning null for getMessage().
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Semaphore#acquire() calls a method which calls another method which can throw new InterruptedException(). No arguments means no message which will result in Throwable returning null for getMessage().


Thanks, Ron! That explains it perfectly. Do you have the source for acquire? I remember being able to step into library routines at one time, but not on my current machine. I must have downloaded something to be able to do that, but can't recall what it was.

Based on your explanation, I'm inclined to see this as a bug. However, it turns out that the javadoc on Throwable.getMessage actually says it may return null. I would never have expected that and wonder if this is an example of a bug becoming a feature by amending the documentation to match the imperfect behavior.
 
Ron McLeod
Marshal
Posts: 4010
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may not be the latest, but it unlikely that it has changed much if at all: Semaphore#acquire
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you install the JDK, there's an option to install the source code as well, and this option is activated by default. Check your JDK folder for a file called src.zip. That's where most of the JSE source files live.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I looked for that. It was present in some of my JDKs, absent in others. Apparently, Oracle is been spotty about including it. I downloaded 1.8.0_66 and it was in that, so I'm back in business.
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Odd, I haven't downloaded a JDK in years that didn't include it.
 
I'm full of tinier men! And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic