Finish ThreadFactory Example Signed-off-by: brunobat <bruno...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/2f636699 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/2f636699 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/2f636699 Branch: refs/heads/master Commit: 2f636699d8ba79e753c37887a60cda50ac749329 Parents: 1d99d91 Author: brunobat <bruno...@gmail.com> Authored: Wed Dec 26 12:14:11 2018 +0000 Committer: brunobat <bruno...@gmail.com> Committed: Wed Dec 26 12:14:11 2018 +0000 ---------------------------------------------------------------------- .../superbiz/executor/ThreadFactoryService.java | 3 +- .../executor/ThreadFactoryServiceTest.java | 50 ++++++++++---------- 2 files changed, 28 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/2f636699/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 629e9db..93df490 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 @@ -97,7 +97,8 @@ public class ThreadFactoryService { // Simulate a long processing task using TimeUnit to sleep. TimeUnit.MILLISECONDS.sleep(taskDurationMs); } catch (InterruptedException e) { - isTerminated.set(false); + isTerminated.set(true); + countDownLatch.countDown(); throw new RuntimeException("Problem while waiting"); } http://git-wip-us.apache.org/repos/asf/tomee/blob/2f636699/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 efe2771..de5ba56 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 @@ -1,5 +1,22 @@ package org.superbiz.executor; +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ShrinkWrap; @@ -17,22 +34,11 @@ import java.util.logging.Logger; import static org.junit.Assert.assertEquals; 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 - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * This demonstrates used managed threads inside a container. + * <br> + * We use CountDownLatch to demonstrate the thread management because it's faster and + * safer that simply using a sleep and hope the other thread has completed. */ @RunWith(Arquillian.class) public class ThreadFactoryServiceTest { @@ -66,15 +72,11 @@ public class ThreadFactoryServiceTest { final CountDownLatch countDownLatch = new CountDownLatch(1); final LongTask longTask = new LongTask(1, 1000000, countDownLatch); - try { - factoryService.asyncHangingTask(longTask); - } catch (RuntimeException e) { - // - } + factoryService.asyncHangingTask(longTask); + countDownLatch.await(200, TimeUnit.MILLISECONDS); - LOGGER.info("task was interrupted and operation was not completed."); + LOGGER.info("task should have been interrupted and its operation not completed."); -// assertTrue(longTask.getIsTerminated()); - assertEquals(1, longTask.getResult()); + assertTrue(longTask.getIsTerminated()); } } \ No newline at end of file