Path: blob/master/test/jdk/java/util/Properties/ConcurrentLoadAndStoreXML.java
41149 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*/2930import java.io.*;31import java.util.Properties;32import java.util.Random;33import java.util.concurrent.Callable;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;36import java.util.concurrent.Future;3738public class ConcurrentLoadAndStoreXML {3940static final Random RAND = new Random();4142static volatile boolean done;4344/**45* Simple task that bashes on storeToXML and loadFromXML until the "done"46* flag is set.47*/48static class Basher implements Callable<Void> {49final Properties props;5051Basher(Properties props) {52this.props = props;53}5455public Void call() throws IOException {56while (!done) {5758// store as XML format59ByteArrayOutputStream out = new ByteArrayOutputStream();60props.storeToXML(out, null, "UTF-8");6162// load from XML format63Properties p = new Properties();64ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());65p.loadFromXML(in);6667// check that the properties are as expected68if (!p.equals(props))69throw new RuntimeException("Properties not equal");70}71return null;72}73}7475public static void main(String[] args) throws Exception {76final int NTASKS = 4 + RAND.nextInt(4);7778// create Bashers with Properties of random keys and values79Basher[] basher = new Basher[NTASKS];80for (int i=0; i<NTASKS; i++) {81Properties props = new Properties();82for (int j=0; j<RAND.nextInt(100); j++) {83String key = "k" + RAND.nextInt(1000);84String value = "v" + RAND.nextInt(1000);85props.put(key, value);86}87basher[i] = new Basher(props);88}8990ExecutorService pool = Executors.newFixedThreadPool(NTASKS);91try {92// kick off the bashers93Future<Void>[] task = new Future[NTASKS];94for (int i=0; i<NTASKS; i++) {95task[i] = pool.submit(basher[i]);96}9798// give them time to interfere with each each99Thread.sleep(2000);100done = true;101102// check the result103for (int i=0; i<NTASKS; i++) {104task[i].get();105}106} finally {107done = true;108pool.shutdown();109}110111}112}113114115