Path: blob/master/test/jdk/java/nio/charset/coders/BashCache.java
41153 views
/*1* Copyright (c) 2010, 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 451727925* @summary Stochastic test of thread-local coder caches26* @key randomness27*/2829import java.nio.*;30import java.nio.charset.*;31import java.util.*;323334public class BashCache {3536private static final int THREADS = 10;37private static final int TRIALS = 1000;3839private static final Charset[] charsets40= new Charset[] {41Charset.forName("US-ASCII"),42Charset.forName("UTF-8"),43Charset.forName("CP1252"),44Charset.forName("UTF-16BE") };4546private static volatile boolean failed = false;4748private static class Basher extends Thread {4950Random rnd = new Random(System.identityHashCode(this));5152public void run() {53for (int i = 0; i < TRIALS; i++) {54Charset cs = charsets[rnd.nextInt(4)];55try {56if (rnd.nextBoolean()) {57cs.encode("hi mom");58} else {59cs.decode(ByteBuffer.wrap(new byte[] {60(byte)'x', (byte)'y',61(byte)'z', (byte)'z',62(byte)'y' }));63}64} catch (Exception x) {65x.printStackTrace();66failed = true;67return;68}69if (rnd.nextBoolean())70Thread.yield();71}72}7374}7576public static void main(String[] args) throws Exception {77Charset cs = Charset.forName("us-ascii");78Basher[] bashers = new Basher[THREADS];79for (int i = 0; i < THREADS; i++) {80bashers[i] = new Basher();81bashers[i].start();82}83for (int i = 0; i < THREADS; i++)84bashers[i].join();85if (failed)86throw new Exception("Test failed");87}8889}909192