@Test
  public void testSynchronousIncrements() throws InterruptedException {

    final String name = getUniqueName();
    final Object key = "KEY";

    final int vmCount = VM.getVMCount();
    final int threadsPerVM = 3;
    final int incrementsPerThread = 10;

    for (int i = 0; i < vmCount; i++) {
      VM vm = VM.getVM(i);
      vm.invoke("Create Region", () -> {
        createRegion(name);
        Region<Object, Object> region = getRootRegion().getSubregion(name);
        region.put(key, 0);
      });
    }

    AsyncInvocation<?>[] invokes = new AsyncInvocation[vmCount];
    for (int i = 0; i < vmCount; i++) {
      invokes[i] = VM.getVM(i).invokeAsync("Start Threads and increment", () -> {
        final ThreadGroup group = new ThreadGroup("Incrementors") {
          @Override
          public void uncaughtException(Thread t, Throwable e) {
            String s = "Uncaught exception in thread " + t;
            fail(s, e);
          }
        };

        Thread[] threads = new Thread[threadsPerVM];
        for (int i1 = 0; i1 < threadsPerVM; i1++) {
          Thread thread = new Thread(group, () -> {
            try {
              final Random rand = new Random(System.identityHashCode(this));
              Region<Object, Integer> region = getRootRegion().getSubregion(name);
              for (int j = 0; j < incrementsPerThread; j++) {
                Thread.sleep(rand.nextInt(30) + 30);

                Lock lock = region.getDistributedLock(key);
                assertThat(lock.tryLock(-1, TimeUnit.MILLISECONDS)).isTrue();

                Integer value = region.get(key);
                Integer oldValue = value;
                if (value == null) {
                  value = 1;

                } else {
                  Integer v = value;
                  value = v + 1;
                }

                assertThat(oldValue).isEqualTo(region.get(key));
                region.put(key, value);
                assertThat(value).isEqualTo(region.get(key));

                lock.unlock();
              }
            } catch (InterruptedException interruptedException) {
              fail("interrupted", interruptedException);
            }

          }, "Incrementer " + i1);
          threads[i1] = thread;
          thread.start();
        }

        for (Thread thread : threads) {
          ThreadUtils.join(thread, 30 * 1000);
        }
      });
    }

    for (int i = 0; i < vmCount; i++) {
      invokes[i].get();
    }

    vm0.invoke("Verify final value", () -> {
      Region region = getRootRegion().getSubregion(name);
      Integer value = (Integer) region.get(key);
      assertThat(value).isNotNull();
      int expected = vmCount * threadsPerVM * incrementsPerThread;
      assertThat(expected).isEqualTo(value.intValue());
    });
  }
Tests that a bunch of threads in a bunch of VMs all atomically incrementing the value of an
entry get the right value.
geode-core/src/distributedTest/java/org/apache/geode/cache30/GlobalRegionDUnitTest.java