B
byoder
Basically the situation is that I have a HashMap (yes I know this is
not synchronized and therefore must do it manually). The HashMap is
used by two threads running concurrently, and one thread is adding
values to the map while the other is iterating over the values. Now
typically I would expect to get the ConcurrentModificationException -
but I have tried to synchronize the object manually without any luck.
Perhaps I don't understand locking, but according to Java 1.5 API this
should work, I wrote the following test class to demonstrait what I am
trying to do:
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class TestIndex {
public static HashMap<Date, Date> values = new HashMap<Date, Date>();
public static void main(String[] args) {
new Thread_1().start();
new Thread_2().start();
}
public static class Thread_1 extends Thread {
public Thread_1() {
super("Thread_1");
this.setDaemon(false);
}
public void run() {
System.out.println("Thread_1 run...");
try {
for (int i=0; i<1000000; i++) {
TestIndex.values.put(new Date(),new Date());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread_1 END");
}
}
public static class Thread_2 extends Thread {
public Thread_2() {
super("Thread_2");
this.setDaemon(false);
}
public void run() {
System.out.println("Thread_2 run...");
try {
for (int i=0; i<1000000; i++) {
Map m = Collections.synchronizedMap(TestIndex.values);
synchronized (m) {
HashMap<Date, Date> newMap = new HashMap<Date, Date>(m);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread_2 END");
}
}
}
not synchronized and therefore must do it manually). The HashMap is
used by two threads running concurrently, and one thread is adding
values to the map while the other is iterating over the values. Now
typically I would expect to get the ConcurrentModificationException -
but I have tried to synchronize the object manually without any luck.
Perhaps I don't understand locking, but according to Java 1.5 API this
should work, I wrote the following test class to demonstrait what I am
trying to do:
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class TestIndex {
public static HashMap<Date, Date> values = new HashMap<Date, Date>();
public static void main(String[] args) {
new Thread_1().start();
new Thread_2().start();
}
public static class Thread_1 extends Thread {
public Thread_1() {
super("Thread_1");
this.setDaemon(false);
}
public void run() {
System.out.println("Thread_1 run...");
try {
for (int i=0; i<1000000; i++) {
TestIndex.values.put(new Date(),new Date());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread_1 END");
}
}
public static class Thread_2 extends Thread {
public Thread_2() {
super("Thread_2");
this.setDaemon(false);
}
public void run() {
System.out.println("Thread_2 run...");
try {
for (int i=0; i<1000000; i++) {
Map m = Collections.synchronizedMap(TestIndex.values);
synchronized (m) {
HashMap<Date, Date> newMap = new HashMap<Date, Date>(m);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread_2 END");
}
}
}