You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geode.apache.org by GitBox <gi...@apache.org> on 2020/04/20 21:05:12 UTC

[GitHub] [geode] prettyClouds commented on a change in pull request #4973: improvement/GEODE-8002 Refactor: Extract class to encapsulate concurrent execution

prettyClouds commented on a change in pull request #4973:
URL: https://github.com/apache/geode/pull/4973#discussion_r411691444



##########
File path: geode-redis/src/integrationTest/java/org/apache/geode/redis/general/LoopingThreads.java
##########
@@ -0,0 +1,88 @@
+/*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.apache.geode.redis.general;
+
+import java.util.Arrays;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+public class LoopingThreads {
+  private final int iterationCount;
+  private final Function<Integer, Object>[] functions;
+
+  @SuppressWarnings("unchecked")
+  public LoopingThreads(int iterationCount,
+      Function... functions) {
+    this.iterationCount = iterationCount;
+    this.functions = (Function<Integer, Object>[]) functions;
+  }
+
+  public void run() {
+    CountDownLatch latch = new CountDownLatch(1);
+    Stream<LoopingThread> loopingThreadStream = Arrays
+        .stream(functions)
+        .map((r) -> new LoopingThread(r, iterationCount, latch))
+        .map((t) -> {
+          t.start();
+          return t;
+        });
+
+    latch.countDown();
+
+    loopingThreadStream.forEach(loopingThread -> {
+      try {
+        loopingThread.join();
+      } catch (InterruptedException e) {
+        throw new RuntimeException(e);
+      }
+    });
+
+  }
+
+  private class LoopingRunnable implements Runnable {
+    private final Function<Integer, Object> runnable;
+    private final int iterationCount;
+    private CountDownLatch startLatch;
+
+    public LoopingRunnable(Function<Integer, Object> runnable, int iterationCount,
+        CountDownLatch startLatch) {
+      this.runnable = runnable;
+      this.iterationCount = iterationCount;
+      this.startLatch = startLatch;
+    }
+
+    @Override
+    public void run() {
+      try {
+        startLatch.await();
+      } catch (InterruptedException e) {
+        throw new RuntimeException(e);
+      }
+      for (int i = 0; i < iterationCount; i++) {
+        runnable.apply(i);
+        Thread.yield();

Review comment:
       That makes sense. I want to leave it like this as the default, though, and if someone wants to change the behavior they can add that functionality.  How do you feel about that?  Or do you think not having this as an option is a dealbreaker.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org