View Javadoc

1   /**
2    * Copyright 2008 Atlassian Pty Ltd 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0 
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package com.atlassian.util.concurrent;
18  
19  import java.util.concurrent.Callable;
20  
21  /**
22   * {@link LockManager} allows {@link Callable callables} and {@link Runnable runnables} to be run
23   * under a lock that is resolved against an input object.
24   * 
25   * @param <T> The input type that we lock on.
26   * @param <D> The stripe type that we stripe locks on.
27   */
28  public interface LockManager<T> {
29  
30      /**
31       * Execute the supplied {@link Callable} under a lock determined by the descriptor.
32       * 
33       * @param <R> the result type
34       * @param descriptor to look up the lock
35       * @param callable the operation to perform under lock
36       * @return whatever the supplied {@link Callable} returns
37       * @throws Exception if the supplied {@link Callable} throws an exception
38       */
39      <R> R withLock(final T descriptor, final Callable<R> callable) throws Exception;
40  
41      /**
42       * Execute the supplied {@link Runnable} under a lock determined by the descriptor.
43       * 
44       * @param descriptor to look up the lock
45       * @param runnable the operation to perform under lock
46       */
47      void withLock(final T descriptor, final Runnable runnable);
48  }