Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8175967

Default Methods for Lock Interface

    Details

      Description

      A DESCRIPTION OF THE REQUEST :
      The Lock interface has 4 locking methods. 3 of these locking methods could be default methods which defer to the 4th method for the actual work. For example...


         public default void lock()
         {
            boolean interrupted;

            interrupted = false;

            try
            {
               while (true)
               {
                  try
                  {
                     if (tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS))
                        return;
                  }
                  catch (InterruptedException e)
                  {
                     interrupted = true;
                  }
               }
            }
            finally
            {
               if (interrupted)
               {
                  Thread.
                     currentThread().
                     interrupt();
               }
            }
         }

         public default void lockInterruptibly() throws InterruptedException
         {
            while (!tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS))
            {
               // nothing to do
            }
         }

         public default boolean tryLock()
         {
            try
            {
               return(tryLock(0, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
               Thread.
                  currentThread().
                  interrupt();

               return(false);
            }
         }


      JUSTIFICATION :
      This will make implementing Lock much easier since only 1 lock method has to be implemented.


        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: