Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/StringInternSync/StringInternSync.java
41155 views
/*1* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress randomness26*27* @summary converted from VM Testbase gc/gctests/StringInternSync.28* VM Testbase keywords: [gc, stress, stressopt, feature_perm_removal_jdk7, nonconcurrent]29* VM Testbase readme:30* The test verifies that String.intern is correctly synchronized.31* Test interns same strings in different threads and verifies that all interned equal32* strings are same objects.33* This test interns a few large strings.34*35* @library /vmTestbase36* /test/lib37* @run main/othervm38* -XX:+UnlockDiagnosticVMOptions39* -XX:+VerifyStringTableAtExit40* gc.gctests.StringInternSync.StringInternSync41* -ms low42*/4344package gc.gctests.StringInternSync;4546import java.util.*;47import java.util.ArrayList;48import java.util.concurrent.locks.ReadWriteLock;49import java.util.concurrent.locks.ReentrantReadWriteLock;50import nsk.share.TestBug;51import nsk.share.TestFailure;52import nsk.share.gc.*;53import nsk.share.gc.gp.MemoryStrategy;54import nsk.share.gc.gp.MemoryStrategyAware;55import nsk.share.gc.gp.string.RandomStringProducer;5657public class StringInternSync extends ThreadedGCTest implements MemoryStrategyAware {5859// The list of strings which will be interned60static final List<String> stringsToIntern = new ArrayList();61// The global container for references to internded strings62static final List<List<String>> internedStrings = new ArrayList<List<String>>();63// Approximate size occupied by all interned strings64long sizeOfAllInteredStrings = 0;65// maximum size of one string66int maxStringSize;67RandomStringProducer gp = new RandomStringProducer();68MemoryStrategy memoryStrategy;69ReadWriteLock rwlock = new ReentrantReadWriteLock();7071@Override72public void setMemoryStrategy(MemoryStrategy memoryStrategy) {73this.memoryStrategy = memoryStrategy;74}7576private class StringGenerator implements Runnable {7778List<String> internedLocal;7980public StringGenerator(List<String> internedLocal) {81this.internedLocal = internedLocal;82}8384public void run() {85try {86rwlock.readLock().lock();87internedLocal.clear();88for (String str : stringsToIntern) {89// Intern copy of string90// and save reference to it91internedLocal.add(new String(str).intern());92}93} finally {94rwlock.readLock().unlock();95}9697// after each iteration 0 thread98// lock our main resource and verify String.intern99if (internedLocal == internedStrings.get(0)) {100try {101rwlock.writeLock().lock();102// We select first list and compare all other with it103// if 2 strings are equal they should be the same "=="104List<String> interned = internedStrings.get(0);105106for (List<String> list : internedStrings) {107if (list == interned) {108continue;109}110if (list.size() == 0) {111continue; // this thread haven't got lock112}113114if (list.size() != interned.size()) {115throw new TestFailure("Size of interned string list differ from origial."116+ " interned " + list.size() + " original " + interned.size());117}118for (int i = 0; i < interned.size(); i++) {119String str = interned.get(i);120if (!str.equals(list.get(i))) {121throw new TestFailure("The interned strings are not the equals.");122}123if (str != list.get(i)) {124throw new TestFailure("The equal interned strings are not the same.");125}126}127list.clear();128129}130interned.clear();131stringsToIntern.clear();132for (long currentSize = 0; currentSize <= sizeOfAllInteredStrings; currentSize++) {133stringsToIntern.add(gp.create(maxStringSize));134currentSize += maxStringSize;135}136} finally {137rwlock.writeLock().unlock();138}139}140}141}142143@Override144public void run() {145sizeOfAllInteredStrings = 10 * 1024 * 1024; // let use 100 * strings of size 10000146maxStringSize = (int) (sizeOfAllInteredStrings / memoryStrategy.getSize(sizeOfAllInteredStrings));147log.debug("The overall size of interned strings : " + sizeOfAllInteredStrings / (1024 * 1024) + "M");148log.debug("The count of interned strings : " + sizeOfAllInteredStrings / maxStringSize);149for (long currentSize = 0; currentSize <= sizeOfAllInteredStrings; currentSize++) {150stringsToIntern.add(gp.create(maxStringSize));151currentSize += maxStringSize;152}153super.run();154}155156@Override157protected Runnable createRunnable(int i) {158ArrayList list = new ArrayList();159internedStrings.add(list);160return new StringGenerator(list);161}162163public static void main(String[] args) {164GC.runTest(new StringInternSync(), args);165}166}167168169