|
Java example source code file (MapLoops.java)
The MapLoops.java Java example source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * However, the following notice accompanied the original version of this * file: * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ /* * @test * @bug 4486658 * @compile -source 1.5 MapLoops.java * @run main/timeout=1600 MapLoops * @summary Exercise multithreaded maps, by default ConcurrentHashMap. * Multithreaded hash table test. Each thread does a random walk * though elements of "key" array. On each iteration, it checks if * table includes key. If absent, with probability pinsert it * inserts it, and if present, with probability premove it removes * it. (pinsert and premove are expressed as percentages to simplify * parsing from command line.) */ import java.util.*; import java.util.concurrent.*; public class MapLoops { static int nkeys = 10000; static int pinsert = 60; static int premove = 2; static int maxThreads = 100; static int nops = 100000; static int removesPerMaxRandom; static int insertsPerMaxRandom; static final ExecutorService pool = Executors.newCachedThreadPool(); static final List<Throwable> throwables = new CopyOnWriteArrayList<Throwable>(); public static void main(String[] args) throws Exception { Class mapClass = null; if (args.length > 0) { try { mapClass = Class.forName(args[0]); } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } } else mapClass = java.util.concurrent.ConcurrentHashMap.class; if (args.length > 1) maxThreads = Integer.parseInt(args[1]); if (args.length > 2) nkeys = Integer.parseInt(args[2]); if (args.length > 3) pinsert = Integer.parseInt(args[3]); if (args.length > 4) premove = Integer.parseInt(args[4]); if (args.length > 5) nops = Integer.parseInt(args[5]); // normalize probabilities wrt random number generator removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); System.out.print("Class: " + mapClass.getName()); System.out.print(" threads: " + maxThreads); System.out.print(" size: " + nkeys); System.out.print(" ins: " + pinsert); System.out.print(" rem: " + premove); System.out.print(" ops: " + nops); System.out.println(); int k = 1; int warmups = 2; for (int i = 1; i <= maxThreads;) { Thread.sleep(100); test(i, nkeys, mapClass); if (warmups > 0) --warmups; else if (i == k) { k = i << 1; i = i + (i >>> 1); } else if (i == 1 && k == 2) { i = k; warmups = 1; } else i = k; } pool.shutdown(); if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) throw new Error(); if (! throwables.isEmpty()) throw new Error (throwables.size() + " thread(s) terminated abruptly."); } static Integer[] makeKeys(int n) { LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); Integer[] key = new Integer[n]; for (int i = 0; i < key.length; ++i) key[i] = new Integer(rng.next()); return key; } static void shuffleKeys(Integer[] key) { Random rng = new Random(); for (int i = key.length; i > 1; --i) { int j = rng.nextInt(i); Integer tmp = key[j]; key[j] = key[i-1]; key[i-1] = tmp; } } static void test(int i, int nkeys, Class mapClass) throws Exception { System.out.print("Threads: " + i + "\t:"); Map<Integer, Integer> map = (Map Other Java examples (source code examples)Here is a short list of links related to this Java MapLoops.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.