Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Properties/ConcurrentLoadAndStoreXML.java
47178 views
/*1* Copyright (c) 2012, 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/* @test24* @bug 800528125* @summary Test that the Properties storeToXML and loadFromXML methods are26* thread safe27* @key randomness28* @run main ConcurrentLoadAndStoreXML29* @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider ConcurrentLoadAndStoreXML3031*/3233import java.io.*;34import java.util.Properties;35import java.util.Random;36import java.util.concurrent.Callable;37import java.util.concurrent.Executors;38import java.util.concurrent.ExecutorService;39import java.util.concurrent.Future;4041public class ConcurrentLoadAndStoreXML {4243static final Random RAND = new Random();4445static volatile boolean done;4647/**48* Simple task that bashes on storeToXML and loadFromXML until the "done"49* flag is set.50*/51static class Basher implements Callable<Void> {52final Properties props;5354Basher(Properties props) {55this.props = props;56}5758public Void call() throws IOException {59while (!done) {6061// store as XML format62ByteArrayOutputStream out = new ByteArrayOutputStream();63props.storeToXML(out, null, "UTF-8");6465// load from XML format66Properties p = new Properties();67ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());68p.loadFromXML(in);6970// check that the properties are as expected71if (!p.equals(props))72throw new RuntimeException("Properties not equal");73}74return null;75}76}7778public static void main(String[] args) throws Exception {79final int NTASKS = 4 + RAND.nextInt(4);8081// create Bashers with Properties of random keys and values82Basher[] basher = new Basher[NTASKS];83for (int i=0; i<NTASKS; i++) {84Properties props = new Properties();85for (int j=0; j<RAND.nextInt(100); j++) {86String key = "k" + RAND.nextInt(1000);87String value = "v" + RAND.nextInt(1000);88props.put(key, value);89}90basher[i] = new Basher(props);91}9293ExecutorService pool = Executors.newFixedThreadPool(NTASKS);94try {95// kick off the bashers96Future<Void>[] task = new Future[NTASKS];97for (int i=0; i<NTASKS; i++) {98task[i] = pool.submit(basher[i]);99}100101// give them time to interfere with each each102Thread.sleep(2000);103done = true;104105// check the result106for (int i=0; i<NTASKS; i++) {107task[i].get();108}109} finally {110done = true;111pool.shutdown();112}113114}115}116117118