Listing 10: File Book.java: Illustrates synchronized methods

class Book
{
   private final String title;
   private final String author;
   private String borrower;

   public Book(String title, String author)
   {
      this.title = title;
      this.author = author;
      borrower = null;
   }

   public synchronized boolean checkOut(String borrower)
   {
      if (isAvailable())
      {
         this.borrower = borrower;
         return true;
      }
      else
         return false;
   }

   public synchronized boolean checkIn()
   {
      if (!isAvailable())
      {
         borrower = null;
         return true;
      }
      else
         return false;
   }

   public String getTitle()
   {
      return title;
   }

   public String getAuthor()
   {
      return author;
   }

   public synchronized boolean isAvailable()
   {
      return borrower == null;
   }

   public synchronized String getBorrower()
   {
      return borrower;
   }
}
— End of Listing —