1. 在使用阻塞等待获取锁的方式中,必须在try代码块之外,并且在加锁方法与try代码块之间没有任何可能抛出异常的方法调用,避免加锁成功后,在finally中无法解锁。

说明一:如果在lock方法与try代码块之间的方法调用抛出异常,那么无法解锁,造成其它线程无法成功获取锁。
说明二:如果lock方法在try代码块之内,可能由于其它方法抛出异常,导致在finally代码块中,unlock对未加锁的对象解锁,它会调用AQS的tryRelease方法(取决于具体实现类),抛出IllegalMonitorStateException异常。
说明三:在Lock对象的lock方法实现中可能抛出unchecked异常,产生的后果与说明二相同。 java.concurrent.LockShouldWithTryFinallyRule.rule.desc

2. 补上遗漏的Override注解
This commit is contained in:
zhuqianchao
2020-08-29 17:22:36 +08:00
parent 50c30259cb
commit 0e9909ffeb
4 changed files with 303 additions and 290 deletions

View File

@@ -105,7 +105,7 @@ public class ConcurrentHashSet<E> extends AbstractSet<E> implements java.io.Seri
@Override
public boolean remove(Object o) {
return map.remove(o) == PRESENT;
return PRESENT.equals(map.remove(o));
}
@Override

View File

@@ -16,73 +16,86 @@ import java.util.concurrent.TimeoutException;
* @author loolly
*/
public class DelegatedExecutorService extends AbstractExecutorService {
private final ExecutorService e;
private final ExecutorService e;
DelegatedExecutorService(ExecutorService executor) {
e = executor;
}
DelegatedExecutorService(ExecutorService executor) {
e = executor;
}
@SuppressWarnings("NullableProblems")
public void execute(Runnable command) {
e.execute(command);
}
@Override
@SuppressWarnings("NullableProblems")
public void execute(Runnable command) {
e.execute(command);
}
public void shutdown() {
e.shutdown();
}
@Override
public void shutdown() {
e.shutdown();
}
@SuppressWarnings("NullableProblems")
public List<Runnable> shutdownNow() {
return e.shutdownNow();
}
@Override
@SuppressWarnings("NullableProblems")
public List<Runnable> shutdownNow() {
return e.shutdownNow();
}
public boolean isShutdown() {
return e.isShutdown();
}
@Override
public boolean isShutdown() {
return e.isShutdown();
}
public boolean isTerminated() {
return e.isTerminated();
}
@Override
public boolean isTerminated() {
return e.isTerminated();
}
@SuppressWarnings("NullableProblems")
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return e.awaitTermination(timeout, unit);
}
@Override
@SuppressWarnings("NullableProblems")
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return e.awaitTermination(timeout, unit);
}
@SuppressWarnings("NullableProblems")
public Future<?> submit(Runnable task) {
return e.submit(task);
}
@Override
@SuppressWarnings("NullableProblems")
public Future<?> submit(Runnable task) {
return e.submit(task);
}
@SuppressWarnings("NullableProblems")
public <T> Future<T> submit(Callable<T> task) {
return e.submit(task);
}
@Override
@SuppressWarnings("NullableProblems")
public <T> Future<T> submit(Callable<T> task) {
return e.submit(task);
}
@SuppressWarnings("NullableProblems")
public <T> Future<T> submit(Runnable task, T result) {
return e.submit(task, result);
}
@Override
@SuppressWarnings("NullableProblems")
public <T> Future<T> submit(Runnable task, T result) {
return e.submit(task, result);
}
@SuppressWarnings("NullableProblems")
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return e.invokeAll(tasks);
}
@Override
@SuppressWarnings("NullableProblems")
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return e.invokeAll(tasks);
}
@SuppressWarnings("NullableProblems")
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
return e.invokeAll(tasks, timeout, unit);
}
@Override
@SuppressWarnings("NullableProblems")
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
return e.invokeAll(tasks, timeout, unit);
}
@SuppressWarnings("NullableProblems")
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException {
return e.invokeAny(tasks);
}
@Override
@SuppressWarnings("NullableProblems")
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException {
return e.invokeAny(tasks);
}
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return e.invokeAny(tasks, timeout, unit);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return e.invokeAny(tasks, timeout, unit);
}
}