package test; import java.util.*; public class Test2 { public static final int _keyRange = 100; public static final int _threadCount = 300; public static final int _repeat = 100000; //-------------------------------------------------------------------------------- public static void main( String[] args ) throws Exception { final long start = System.currentTimeMillis(); final Map map = new HashMap(); Runtime.getRuntime().addShutdownHook( new Thread( new Runnable() { public void run() { System.out.println( System.currentTimeMillis() - start ); System.out.println( map ); } } ) ); for( int i = 0; i < _threadCount; ++i ) { new Thread( new Runnable() { public void run() { for( int k = 0; k < _repeat; ++k ) { Random random = new Random(); String key = "key" + random.nextInt( _keyRange ); synchronized( map ) { Integer value = ( Integer )map.get( key ); if( value != null ) { int oldValue = value.intValue(); map.put( key, new Integer( oldValue + 1 ) ); } else { map.put( key, new Integer( 1 ) ); } } } } } ).start(); } } //-------------------------------------------------------------------------------- }