alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (TestStringIntern.java)

This example Lucene source code file (TestStringIntern.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

interruptedexception, interruptedexception, lucenetestcase, override, random, random, random_multiplier, string, string, teststringintern, thread, thread, util

The Lucene TestStringIntern.java source code

/**
 * 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.lucene.util;
import java.util.Random;

public class TestStringIntern extends LuceneTestCase {
  String[] testStrings;
  String[] internedStrings;

  private String randStr(int len) {
    char[] arr = new char[len];
    for (int i=0; i<len; i++) {
      arr[i] = (char)('a' + random.nextInt(26));
    }
    return new String(arr);
  }

  private void makeStrings(int sz) {
    testStrings = new String[sz];
    internedStrings = new String[sz];
    for (int i=0; i<sz; i++) {
      testStrings[i] = randStr(random.nextInt(8)+3);
    }
  }

  public void testStringIntern() throws InterruptedException {
    makeStrings(1024*10);  // something greater than the capacity of the default cache size
    // makeStrings(100);  // realistic for perf testing
    int nThreads = 20;
    // final int iter=100000;
    final int iter = 1000000 * RANDOM_MULTIPLIER;
    
    // try native intern
    // StringHelper.interner = new StringInterner();

    Thread[] threads = new Thread[nThreads];
    for (int i=0; i<nThreads; i++) {
      final int seed = i;
      threads[i] = new Thread() {
        @Override
        public void run() {
          Random rand = new Random(seed);
          String[] myInterned = new String[testStrings.length];
          for (int j=0; j<iter; j++) {
            int idx = rand.nextInt(testStrings.length);
            String s = testStrings[idx];
            if (rand.nextBoolean()) s = new String(s); // make a copy half of the time
            String interned = StringHelper.intern(s);
            String prevInterned = myInterned[idx];
            String otherInterned = internedStrings[idx];

            // test against other threads
            if (otherInterned != null && otherInterned != interned) {
              fail();
            }
            internedStrings[idx] = interned;

            // test against local copy
            if (prevInterned != null && prevInterned != interned) {
              fail();
            }
            myInterned[idx] = interned;
          }
        }
      };

      threads[i].start();
    }

    for (int i=0; i<nThreads; i++) {
      threads[i].join();
    }
  }
}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene TestStringIntern.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.