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 @Override
public boolean remove(Object o) { public boolean remove(Object o) {
return map.remove(o) == PRESENT; return PRESENT.equals(map.remove(o));
} }
@Override @Override

View File

@ -22,65 +22,78 @@ public class DelegatedExecutorService extends AbstractExecutorService {
e = executor; e = executor;
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public void execute(Runnable command) { public void execute(Runnable command) {
e.execute(command); e.execute(command);
} }
@Override
public void shutdown() { public void shutdown() {
e.shutdown(); e.shutdown();
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public List<Runnable> shutdownNow() { public List<Runnable> shutdownNow() {
return e.shutdownNow(); return e.shutdownNow();
} }
@Override
public boolean isShutdown() { public boolean isShutdown() {
return e.isShutdown(); return e.isShutdown();
} }
@Override
public boolean isTerminated() { public boolean isTerminated() {
return e.isTerminated(); return e.isTerminated();
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return e.awaitTermination(timeout, unit); return e.awaitTermination(timeout, unit);
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public Future<?> submit(Runnable task) { public Future<?> submit(Runnable task) {
return e.submit(task); return e.submit(task);
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public <T> Future<T> submit(Callable<T> task) { public <T> Future<T> submit(Callable<T> task) {
return e.submit(task); return e.submit(task);
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public <T> Future<T> submit(Runnable task, T result) { public <T> Future<T> submit(Runnable task, T result) {
return e.submit(task, result); return e.submit(task, result);
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException { public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return e.invokeAll(tasks); return e.invokeAll(tasks);
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
return e.invokeAll(tasks, timeout, unit); return e.invokeAll(tasks, timeout, unit);
} }
@Override
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException { throws InterruptedException, ExecutionException {
return e.invokeAny(tasks); return e.invokeAny(tasks);
} }
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException { throws InterruptedException, ExecutionException, TimeoutException {
return e.invokeAny(tasks, timeout, unit); return e.invokeAny(tasks, timeout, unit);

View File

@ -52,8 +52,8 @@ public class TaskTable implements Serializable {
*/ */
public TaskTable add(String id, CronPattern pattern, Task task) { public TaskTable add(String id, CronPattern pattern, Task task) {
final Lock writeLock = lock.writeLock(); final Lock writeLock = lock.writeLock();
try {
writeLock.lock(); writeLock.lock();
try {
if (ids.contains(id)) { if (ids.contains(id)) {
throw new CronException("Id [{}] has been existed!", id); throw new CronException("Id [{}] has been existed!", id);
} }
@ -75,8 +75,8 @@ public class TaskTable implements Serializable {
*/ */
public List<String> getIds() { public List<String> getIds() {
final Lock readLock = lock.readLock(); final Lock readLock = lock.readLock();
try {
readLock.lock(); readLock.lock();
try {
return Collections.unmodifiableList(this.ids); return Collections.unmodifiableList(this.ids);
} finally { } finally {
readLock.unlock(); readLock.unlock();
@ -91,8 +91,8 @@ public class TaskTable implements Serializable {
*/ */
public List<CronPattern> getPatterns() { public List<CronPattern> getPatterns() {
final Lock readLock = lock.readLock(); final Lock readLock = lock.readLock();
try {
readLock.lock(); readLock.lock();
try {
return Collections.unmodifiableList(this.patterns); return Collections.unmodifiableList(this.patterns);
} finally { } finally {
readLock.unlock(); readLock.unlock();
@ -107,8 +107,8 @@ public class TaskTable implements Serializable {
*/ */
public List<Task> getTasks() { public List<Task> getTasks() {
final Lock readLock = lock.readLock(); final Lock readLock = lock.readLock();
try {
readLock.lock(); readLock.lock();
try {
return Collections.unmodifiableList(this.tasks); return Collections.unmodifiableList(this.tasks);
} finally { } finally {
readLock.unlock(); readLock.unlock();
@ -122,8 +122,8 @@ public class TaskTable implements Serializable {
*/ */
public void remove(String id) { public void remove(String id) {
final Lock writeLock = lock.writeLock(); final Lock writeLock = lock.writeLock();
try {
writeLock.lock(); writeLock.lock();
try {
final int index = ids.indexOf(id); final int index = ids.indexOf(id);
if (index > -1) { if (index > -1) {
tasks.remove(index); tasks.remove(index);
@ -146,8 +146,8 @@ public class TaskTable implements Serializable {
*/ */
public boolean updatePattern(String id, CronPattern pattern) { public boolean updatePattern(String id, CronPattern pattern) {
final Lock writeLock = lock.writeLock(); final Lock writeLock = lock.writeLock();
try {
writeLock.lock(); writeLock.lock();
try {
final int index = ids.indexOf(id); final int index = ids.indexOf(id);
if (index > -1) { if (index > -1) {
patterns.set(index, pattern); patterns.set(index, pattern);
@ -168,8 +168,8 @@ public class TaskTable implements Serializable {
*/ */
public Task getTask(int index) { public Task getTask(int index) {
final Lock readLock = lock.readLock(); final Lock readLock = lock.readLock();
try {
readLock.lock(); readLock.lock();
try {
return tasks.get(index); return tasks.get(index);
} finally { } finally {
readLock.unlock(); readLock.unlock();
@ -200,8 +200,8 @@ public class TaskTable implements Serializable {
*/ */
public CronPattern getPattern(int index) { public CronPattern getPattern(int index) {
final Lock readLock = lock.readLock(); final Lock readLock = lock.readLock();
try {
readLock.lock(); readLock.lock();
try {
return patterns.get(index); return patterns.get(index);
} finally { } finally {
readLock.unlock(); readLock.unlock();
@ -250,8 +250,8 @@ public class TaskTable implements Serializable {
*/ */
public void executeTaskIfMatch(long millis) { public void executeTaskIfMatch(long millis) {
final Lock readLock = lock.readLock(); final Lock readLock = lock.readLock();
try {
readLock.lock(); readLock.lock();
try {
executeTaskIfMatchInternal(millis); executeTaskIfMatchInternal(millis);
} finally { } finally {
readLock.unlock(); readLock.unlock();

View File

@ -140,8 +140,8 @@ public class RC4 implements Serializable {
*/ */
public byte[] crypt(final byte[] msg) { public byte[] crypt(final byte[] msg) {
final ReadLock readLock = this.lock.readLock(); final ReadLock readLock = this.lock.readLock();
readLock.lock();
byte[] code; byte[] code;
readLock.lock();
try { try {
final int[] sbox = this.sbox.clone(); final int[] sbox = this.sbox.clone();
code = new byte[msg.length]; code = new byte[msg.length];