You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2018/12/26 14:02:33 UTC
[09/13] tomee git commit: use countdownlatch
use countdownlatch
Signed-off-by: brunobat <br...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/cc2cd4cf
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/cc2cd4cf
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/cc2cd4cf
Branch: refs/heads/master
Commit: cc2cd4cf5fcf8b44829d75461de544003ed8e1db
Parents: fad8df9
Author: brunobat <br...@gmail.com>
Authored: Mon Dec 24 16:08:54 2018 +0000
Committer: brunobat <br...@gmail.com>
Committed: Mon Dec 24 16:08:54 2018 +0000
----------------------------------------------------------------------
.../superbiz/executor/ThreadFactoryService.java | 28 ++++++++------------
1 file changed, 11 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/cc2cd4cf/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
----------------------------------------------------------------------
diff --git a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
index 223b592..629e9db 100644
--- a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
+++ b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
@@ -5,6 +5,7 @@ import javax.enterprise.concurrent.ManagedThreadFactory;
import javax.enterprise.context.RequestScoped;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
/**
@@ -34,47 +35,34 @@ public class ThreadFactoryService {
/**
* Happy path.
*
- * @param value to compute
- * @return The thread we created
+ * @param longTask to compute
*/
- public int asyncTask(final int value) throws InterruptedException {
+ public void asyncTask(final LongTask longTask) throws InterruptedException {
LOGGER.info("Create asyncTask");
- final CountDownLatch countDownLatch = new CountDownLatch(1);
- final LongTask longTask = new LongTask(value, 1000000, countDownLatch);
final Thread thread = factory.newThread(longTask);
thread.setName("pretty asyncTask");
thread.start();
-
- countDownLatch.await(200, TimeUnit.MILLISECONDS);
-
- return longTask.getResult();
}
/**
* Example where we have to stop a thread.
*
- * @param value
- * @return The thread we created
+ * @param longTask
* @throws InterruptedException
*/
- public int asyncHangingTask(final int value) throws InterruptedException {
+ public void asyncHangingTask(final Runnable longTask) {
LOGGER.info("Create asyncHangingTask");
- final CountDownLatch countDownLatch = new CountDownLatch(1);
- final LongTask longTask = new LongTask(value, 1000000, countDownLatch);
final Thread thread = factory.newThread(longTask);
thread.setName("pretty asyncHangingTask");
thread.start();
- countDownLatch.await(200, TimeUnit.MILLISECONDS);
-
if (thread.isAlive()) {
// This will cause any wait in the thread to resume.
// This will call the InterruptedException block in the longRunnableTask method.
thread.interrupt();
}
- return longTask.getResult();
}
/**
@@ -85,6 +73,7 @@ public class ThreadFactoryService {
private final long taskDurationMs;
private final CountDownLatch countDownLatch;
private int result;
+ private AtomicBoolean isTerminated = new AtomicBoolean(false);
public LongTask(final int value,
final long taskDurationMs,
@@ -98,12 +87,17 @@ public class ThreadFactoryService {
return result;
}
+ public boolean getIsTerminated() {
+ return isTerminated.get();
+ }
+
@Override
public void run() {
try {
// Simulate a long processing task using TimeUnit to sleep.
TimeUnit.MILLISECONDS.sleep(taskDurationMs);
} catch (InterruptedException e) {
+ isTerminated.set(false);
throw new RuntimeException("Problem while waiting");
}