Java Thread dies without notification
<p>I was testing the concurrency with a HashMap (for academic purposes). My goal was to throw some kind of exception related to modifying the structure of the map from multiple threads.</p>

<p>It ended up that the threads that have the task to put stuff in my hash map inexplicably die without throwing any kind of exception...</p>

<pre><code>public class Test {
  public static void main(String[] args) {
    Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
      public void uncaughtException(Thread th, Throwable ex) {
        System.out.println("Uncaught exception: " + ex);
      }
    };

    Thread a = new Thread(new MyTask("A"));
    Thread b = new Thread(new MyTask("B"));
    Thread c = new Thread(new MyTask("C"));
    Thread d = new Thread(new MyTask("D"));

    a.setUncaughtExceptionHandler(h);
    b.setUncaughtExceptionHandler(h);
    c.setUncaughtExceptionHandler(h);
    d.setUncaughtExceptionHandler(h);

    a.start();
    b.start();
    c.start();
    d.start();
    while (true) {
      System.out.println(MyTask.map.size());
    }
  }
}

public class MyTask implements Runnable {
  static AtomicInteger counter = new AtomicInteger(0);
  static Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
  String name;

  MyTask(String _name) {
    name = _name;
  }

  @Override
  public void run() {
    while (true) {
      counter.incrementAndGet();

      try {
        map.put(name + "_" + counter.get(), counter.get());
      } catch (RuntimeException e) {
        e.printStackTrace();
      }

      System.out.println(name + "_" + counter.get());
    }
   }
 }
</code></pre>

<p>If you run this, you'll notice that the threads will stop printing at some point and the size of the map does not change anymore. If you switch to a ConcurrentHashMap, everything works fine (it keeps printing forever).</p>

<p>Please let me know how to catch such an exception / error!</p>