1 /* 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4517279 26 * @summary Stochastic test of thread-local coder caches 27 */ 28 29 import java.nio.*; 30 import java.nio.charset.*; 31 import java.util.*; 32 33 34 public class BashCache { 35 36 private static final int THREADS = 10; 37 private static final int TRIALS = 1000; 38 39 private static final Charset[] charsets 40 = new Charset[] { 41 Charset.forName("US-ASCII"), 42 Charset.forName("UTF-8"), 43 Charset.forName("CP1252"), 44 Charset.forName("UTF-16BE") }; 45 46 private static volatile boolean failed = false; 47 48 private static class Basher extends Thread { 49 50 Random rnd = new Random(System.identityHashCode(this)); 51 52 public void run() { 53 for (int i = 0; i < TRIALS; i++) { 54 Charset cs = charsets[rnd.nextInt(4)]; 55 try { 56 if (rnd.nextBoolean()) { 57 cs.encode("hi mom"); 58 } else { 59 cs.decode(ByteBuffer.wrap(new byte[] { 60 (byte)'x', (byte)'y', 61 (byte)'z', (byte)'z', 62 (byte)'y' })); 63 } 64 } catch (Exception x) { 65 x.printStackTrace(); 66 failed = true; 67 return; 68 } 69 if (rnd.nextBoolean()) 70 Thread.yield(); 71 } 72 } 73 74 } 75 76 public static void main(String[] args) throws Exception { 77 Charset cs = Charset.forName("us-ascii"); 78 Basher[] bashers = new Basher[THREADS]; 79 for (int i = 0; i < THREADS; i++) { 80 bashers[i] = new Basher(); 81 bashers[i].start(); 82 } 83 for (int i = 0; i < THREADS; i++) 84 bashers[i].join(); 85 if (failed) 86 throw new Exception("Test failed"); 87 } 88 89 }