Skip to content

Commit 6f14721

Browse files
[TH2-5267] Use interrupted await in awaitScheduled()
1 parent 825a9c6 commit 6f14721

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

gradle.properties

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
#
2-
# Copyright 2022 Exactpro (Exactpro Systems Limited)
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-
release_version = 0.1.2
1+
release_version = 0.1.3
182
description = "Task managenet utility classes"
193
vcs_url = https://github.com/th2-net/th2-task-utils

src/main/java/com/exactpro/th2/taskutils/BlockingScheduledRetryableTaskQueue.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 Exactpro (Exactpro Systems Limited)
2+
* Copyright 2020-2025 Exactpro (Exactpro Systems Limited)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.PriorityQueue;
2121
import java.util.Queue;
2222
import java.util.Set;
23+
import java.util.concurrent.atomic.AtomicLong;
2324
import java.util.concurrent.locks.Condition;
2425
import java.util.concurrent.locks.Lock;
2526
import java.util.concurrent.locks.ReentrantLock;
@@ -32,7 +33,7 @@ public class BlockingScheduledRetryableTaskQueue<V> {
3233
private final Queue<ScheduledRetryableTask<V>> taskQueue;
3334
private final Set<ScheduledRetryableTask<V>> taskSet;
3435

35-
private volatile long dataSize;
36+
private final AtomicLong dataSize;
3637
private final Lock lock;
3738
private final Condition addition;
3839
private final Condition removal;
@@ -67,6 +68,7 @@ public BlockingScheduledRetryableTaskQueue(int maxTaskCount, long maxDataSize, R
6768

6869
taskQueue = new PriorityQueue<>(ScheduledRetryableTask::compareOrder);
6970
taskSet = new HashSet<>();
71+
dataSize = new AtomicLong(0);
7072
lock = new ReentrantLock();
7173
addition = lock.newCondition();
7274
removal = lock.newCondition();
@@ -87,9 +89,9 @@ public void submit(ScheduledRetryableTask<V> task) {
8789
throw new IllegalStateException("Task has been already submitted");
8890

8991
while (true) {
90-
long capacityLeft = maxDataSize - dataSize;
92+
long capacityLeft = maxDataSize - dataSize.get();
9193
if (capacityLeft >= task.getPayloadSize() && taskSet.size() < maxTaskCount) {
92-
dataSize += task.getPayloadSize();
94+
dataSize.addAndGet(task.getPayloadSize());
9395
addTask(task);
9496
break;
9597
} else {
@@ -141,7 +143,7 @@ public void complete(ScheduledRetryableTask<V> task) {
141143
throw new IllegalStateException("Task to complete has not been submitted previously");
142144
taskSet.remove(task);
143145

144-
dataSize -= task.getPayloadSize();
146+
dataSize.addAndGet(-task.getPayloadSize());
145147
removal.signalAll();
146148
} finally {
147149
lock.unlock();
@@ -165,7 +167,7 @@ public ScheduledRetryableTask<V> take() {
165167
lock.lock();
166168
try {
167169
while (true) {
168-
if (taskQueue.size() > 0)
170+
if (!taskQueue.isEmpty())
169171
return taskQueue.poll();
170172
else
171173
addition.awaitUninterruptibly();
@@ -195,8 +197,8 @@ public ScheduledRetryableTask<V> awaitScheduled() throws InterruptedException {
195197
lock.lock();
196198
try {
197199
while (true) {
198-
if (taskQueue.size() == 0)
199-
addition.awaitUninterruptibly();
200+
if (taskQueue.isEmpty())
201+
addition.await();
200202
else {
201203
ScheduledRetryableTask<V> job = taskQueue.peek();
202204
long now = System.nanoTime();
@@ -279,7 +281,7 @@ public void setMaxDataSize(long value) {
279281
public long getUsedDataSize() {
280282
lock.lock();
281283
try {
282-
return dataSize;
284+
return dataSize.get();
283285
} finally {
284286
lock.unlock();
285287
}

0 commit comments

Comments
 (0)