Java Code Examples for io.netty.util.internal.PlatformDependent#newConcurrentHashMap()

The following examples show how to use io.netty.util.internal.PlatformDependent#newConcurrentHashMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RendezvousHashTest.java    From xrpc with Apache License 2.0 5 votes vote down vote up
@Test
public void get_shouldReturnExpectedNumberOfHashesOnAllRuns() throws Exception {
  List<String> hostList = new ArrayList<>();
  Map<String, List<String>> hostToMatchingHashes = PlatformDependent.newConcurrentHashMap();
  int totalHosts = 100;
  for (int i = 0; i < totalHosts; i++) {
    hostList.add(("Host" + i));
    hostToMatchingHashes.put(("Host" + i), new ArrayList<>());
  }

  RendezvousHash<CharSequence> rendezvousHash =
      new RendezvousHash<>(Funnels.stringFunnel(Charset.defaultCharset()), hostList);

  int totalGetsToRun = 100000;
  int hashesToMatch = 3;
  Random random = new Random();
  for (int i = 0; i < totalGetsToRun; i++) {
    String randomNumberString = (Integer.toString(random.nextInt(MAX_RANDOM_NUMBER)));
    List<CharSequence> hosts = rendezvousHash.get(randomNumberString.getBytes(), hashesToMatch);
    hosts.forEach(host -> hostToMatchingHashes.get(host).add(randomNumberString));
  }

  Double averageMatchingHashesPerHost =
      hostToMatchingHashes.values().stream().mapToInt(List::size).average().orElse(-1);

  int expectedAverage = hashesToMatch * totalGetsToRun / totalHosts;
  assertEquals(expectedAverage, averageMatchingHashesPerHost.intValue());
}
 
Example 2
Source File: UniqueNameTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompareNames() {
    UniqueName one = registerName("One");
    UniqueName two = registerName("Two");

    ConcurrentMap<String, Boolean> mapTwo = PlatformDependent.newConcurrentHashMap();

    UniqueName three = new UniqueName(mapTwo, "One");

    assertSame(one.compareTo(one), 0);
    assertSame(one.compareTo(two), -5);
    assertSame(one.compareTo(three), -1);
    assertSame(three.compareTo(one), 1);
}
 
Example 3
Source File: RendezvousHashUnitTest.java    From xio with Apache License 2.0 5 votes vote down vote up
@Test
public void get() throws Exception {
  List<String> nodeList = new ArrayList<>();
  Map<String, List<String>> mm = PlatformDependent.newConcurrentHashMap();
  for (int i = 0; i < 100; i++) {
    nodeList.add(("Host" + i));
    mm.put(("Host" + i), new ArrayList<>());
  }

  RendezvousHash<CharSequence> rendezvousHash =
      new RendezvousHash<>(Funnels.stringFunnel(Charset.defaultCharset()), nodeList);
  Random r = new Random();
  for (int i = 0; i < 100000; i++) {
    String thing = (Integer.toString(r.nextInt(123456789)));
    List<CharSequence> hosts = rendezvousHash.get(thing.getBytes(), 3);
    hosts.forEach(
        xs -> {
          mm.get(xs).add(thing);
        });
  }

  List<Integer> xx = new ArrayList<>();
  mm.keySet()
      .forEach(
          xs -> {
            xx.add(mm.get(xs).size());
          });

  Double xd = xx.stream().mapToInt(x -> x).average().orElse(-1);
  assertEquals(3000, xd.intValue());
}
 
Example 4
Source File: HttpResponseHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public HttpResponseHandler() {
    // Use a concurrent map because we add and iterate from the main thread (just for the purposes of the example),
    // but Netty also does a get on the map when messages are received in a EventLoop thread.
    streamidPromiseMap = PlatformDependent.newConcurrentHashMap();
}
 
Example 5
Source File: Http2ResponseHandler.java    From product-microgateway with Apache License 2.0 4 votes vote down vote up
public Http2ResponseHandler() {
    // Use a concurrent map because we add and iterate from the main thread (just for the purposes of the example),
    // but Netty also does a get on the map when messages are received in a EventLoop thread.
    streamidPromiseMap = PlatformDependent.newConcurrentHashMap();
}
 
Example 6
Source File: Http2ClientChannelHandler.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
public Http2ClientChannelHandler() {
    // Use a concurrent map because we add and iterate from the main thread (just for the purposes of the example),
    // but Netty also does a get on the map when messages are received in a EventLoop thread.
    streamIdPromiseMap = PlatformDependent.newConcurrentHashMap();
}
 
Example 7
Source File: UniqueNameTest.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Before
public void initializeTest() {
    names = PlatformDependent.newConcurrentHashMap();
}
 
Example 8
Source File: ClientPool.java    From xio with Apache License 2.0 4 votes vote down vote up
public ClientPool(int maxSizePerAddress) {
  this.maxSizePerAddress = maxSizePerAddress;
  this.clientPool = PlatformDependent.newConcurrentHashMap();
}
 
Example 9
Source File: ClassResolvers.java    From netty-4.1.22 with Apache License 2.0 3 votes vote down vote up
/**
 * non-agressive concurrent cache
 * good for shared cache, when we're worried about class unloading
 *
 * @param classLoader - specific classLoader to use, or null if you want to revert to default
 * @return new instance of class resolver
 */
public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
    return new CachingClassResolver(
            new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
            new WeakReferenceMap<String, Class<?>>(
                    PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
}
 
Example 10
Source File: ClassResolvers.java    From netty-4.1.22 with Apache License 2.0 3 votes vote down vote up
/**
 * agressive concurrent cache
 * good for shared cache, when we're not worried about class unloading
 *
 * @param classLoader - specific classLoader to use, or null if you want to revert to default
 * @return new instance of class resolver
 */
public static ClassResolver softCachingConcurrentResolver(ClassLoader classLoader) {
    return new CachingClassResolver(
            new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
            new SoftReferenceMap<String, Class<?>>(
                    PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
}
 
Example 11
Source File: ClassResolvers.java    From netty4.0.27Learn with Apache License 2.0 3 votes vote down vote up
/**
 * non-agressive concurrent cache
 * good for shared cache, when we're worried about class unloading
 *
 * @param classLoader - specific classLoader to use, or null if you want to revert to default
 * @return new instance of class resolver
 */
public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
    return new CachingClassResolver(
            new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
            new WeakReferenceMap<String, Class<?>>(
                    PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
}
 
Example 12
Source File: ClassResolvers.java    From netty4.0.27Learn with Apache License 2.0 3 votes vote down vote up
/**
 * agressive concurrent cache
 * good for shared cache, when we're not worried about class unloading
 *
 * @param classLoader - specific classLoader to use, or null if you want to revert to default
 * @return new instance of class resolver
 */
public static ClassResolver softCachingConcurrentResolver(ClassLoader classLoader) {
    return new CachingClassResolver(
            new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
            new SoftReferenceMap<String, Class<?>>(
                    PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
}