Repository: tomee Updated Branches: refs/heads/master 1ff8ad889 -> 113644b66
TOMEE-2301 - Add ThreadFactory example Signed-off-by: brunobat <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/92b291b1 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/92b291b1 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/92b291b1 Branch: refs/heads/master Commit: 92b291b1d652734fbd87c75d76eeae8d95d0617c Parents: 2aaca11 Author: brunobat <[email protected]> Authored: Mon Dec 17 11:30:16 2018 +0000 Committer: brunobat <[email protected]> Committed: Mon Dec 17 11:30:16 2018 +0000 ---------------------------------------------------------------------- .../superbiz/executor/ThreadFactoryService.java | 17 ++++++++++++++++- .../executor/ThreadFactoryServiceTest.java | 19 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/92b291b1/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 17086e5..6847c54 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 @@ -32,11 +32,26 @@ public class ThreadFactoryService { @Resource private ManagedThreadFactory factory; - public void asyncTask(final int value) { + public Thread asyncTask(final int value) { LOGGER.info("Create asyncTask"); final Thread thread = factory.newThread(longRunnableTask(value, 100, null)); thread.setName("pretty asyncTask"); thread.start(); + return thread; + } + + public Thread asyncHangingTask(final int value) throws InterruptedException { + LOGGER.info("Create asyncTask"); + final Thread thread = factory.newThread(longRunnableTask(value, 1000000, null)); + thread.setName("pretty asyncTask"); + thread.start(); + TimeUnit.MILLISECONDS.sleep(50); + 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 thread; } /** http://git-wip-us.apache.org/repos/asf/tomee/blob/92b291b1/examples/concurrency-utils/src/test/java/org/superbiz/executor/ThreadFactoryServiceTest.java ---------------------------------------------------------------------- diff --git a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ThreadFactoryServiceTest.java b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ThreadFactoryServiceTest.java index 8518c28..e2e15c6 100644 --- a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ThreadFactoryServiceTest.java +++ b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ThreadFactoryServiceTest.java @@ -12,6 +12,10 @@ import javax.inject.Inject; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -46,8 +50,21 @@ public class ThreadFactoryServiceTest { @Test public void asyncTask() throws InterruptedException { - factoryService.asyncTask(1); + final Thread thread = factoryService.asyncTask(1); + assertTrue(thread.isAlive()); + + LOGGER.info("Do something else"); + TimeUnit.MILLISECONDS.sleep(200); + + assertFalse(thread.isAlive()); + } + + @Test + public void asyncHangingTask() throws InterruptedException { + + final Thread thread = factoryService.asyncHangingTask(1); LOGGER.info("Do something else"); TimeUnit.MILLISECONDS.sleep(200); + assertFalse(thread.isAlive()); } } \ No newline at end of file
