import java.util.Iterator; import java.util.Random; import java.util.Vector; public class VectorThrasher extends Thread { private static Random _random = new Random(); private static final int VECTOR_FILL_LENGTH = 25; public static void say( String msg ) { if (false) { System.out.println( "VectorThrahser: " + Thread.currentThread().getName() + ": " + msg ); } } public void run() { int theKeyToMadness = 10; Vector v = null; while (true) { if (v == null) { v = new Vector(); } if (v.size() != 0) { // remove everything from the vector. get a monitor on ourselves for the // hell of it. synchronized (this) { say( "Clearing out my vector" ); for (Iterator i = v.iterator(); i.hasNext();) { i.next(); i.remove(); } } } else { synchronized (this) { say( "Filling up my vector" ); for (int i = 0; i < VECTOR_FILL_LENGTH; ++i) { v.add( new Integer(_random.nextInt()) ); } } } // re-assigning the vector is important to reproduce this problem. it will // keep the hash sycn table busy by hopefully adding new heavy locks into the // chain. Without this re-assignment the heavy lock chains will remain in a // steady state. if (--theKeyToMadness == 0) { v = null; theKeyToMadness = 10; } } } }