Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two questions uppon a deitel's project

 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a deitel book and I am in the chapter 26 about multithreading.
There is a project there with the following code :

SharedBufferTest.java



Buffer.java



Producer.java



Consumer.java



UnsynchronizedBuffer.java



My first question is why we declare the InterruptedException in the interface and the second question is when we can use object without declaration as we do in SharedBufferTest.java with Producer and Consumer objects.
Thank you.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Petas wrote:My first question is why we declare the InterruptedException in the interface


Declaring the InterruptedException indicates the task may take some time, may block execution, and may be interrupted by some other process. The InterruptedException is a checked exception, which means it needs to be handled by client code, and having as part of the method signature in the interface suggests to users of the interface that they need to handle cases where the InterruptedException may occur. Not all implementations will use throw the exception, but perhaps one may throw it, so the caller has to be able to handle it.

and the second question is when we can use object without declaration as we do in SharedBufferTest.java with Producer and Consumer objects.


You can do it whenever you need to create an Object but don't need to hold on to a local reference to it. The main method in the test class needs to create the Producer and Consumer in order to call the 'execute' command, but main method doesn't do anything else with the Producer/Consumer objects. The main method just needs to pass the references to other classes/methods. So why hold on to a reference itself?
 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the author is doing so because he uses the same interface with other class implementations provided by java api that have corresponding methods thate throw InterruptedException errors like ArrayBlockingQueue put() and take().
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Petas wrote:Probably the author is doing so because he uses the same interface with other class implementations provided by java api that have corresponding methods thate throw InterruptedException errors like ArrayBlockingQueue put() and take().


Possibly, but that is an implementation detail. All it says for certain is that the task can be interrupted via Thread#interrupt(), and that the method itself does not handle the interruption, the caller needs to.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic