Add ArrayBlockingQueueWithShutdown.tryPut(E)

This commit is contained in:
Florian Schmaus 2018-05-16 07:57:50 +02:00
parent 59ac41faef
commit c7697ea9d0
1 changed files with 20 additions and 1 deletions

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Florian Schmaus * Copyright 2014-2018 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -263,6 +263,25 @@ public class ArrayBlockingQueueWithShutdown<E> extends AbstractQueue<E> implemen
} }
} }
public boolean tryPut(E e) {
checkNotNull(e);
boolean locked = lock.tryLock();
if (!locked) {
return false;
}
try {
if (isShutdown || isFull()) {
return false;
}
insert(e);
return true;
} finally {
lock.unlock();
}
}
@Override @Override
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
checkNotNull(e); checkNotNull(e);