Hello everyone. I'm having hard time to make my program work correctly. In brief, my program consists of several initial threads: Main, MessageReceiver, Scheduler (used Quartz package) and two types of threads scheduled by the Scheduler thread: TraceReader and ScheduledEvent. Now, when TraceReader is fired, it reads a special trace file and schedules events with start time, repeat interval (500ms to 1 sec) and end time. Currently, about 140 events can be scheduled to fire at the same time which causes lots of ConcurrentModificationException errors. Now some code:
public class Client { //main class
public static volatile HashMap<Integer, Request> requests;
public static class Request{
String node;
int file_id;
long sbyte;
int length;
int pc_id;
public Request(){
}
}
public static synchronized void insertRequest(int req_nr, String node, int file_id, long sbyte, int length, int pc_id) {
Request tempr = new Request();
tempr.node = node;
tempr.file_id = file_id;
tempr.sbyte = sbyte;
tempr.length = length;
tempr.pc_id = pc_id;
requests.put(req_nr, tempr);
}
public static synchronized void doSynchronized(int req_nr, String node, int file_id, long sbyte, int length, int pc_id) {
reqnr++;
String r = "P" + reqnr + "," + file_id + "," + Long.toString(sbyte) + "," + length;
insertRequest(Client.reqnr, node, file_id, sbyte, length, pc_id);
}
public class ScheduledEvent implements Job {
public synchronized boolean isRequested(long sbyte, int length, int file_id, String node) {
Request req;
Iterator<Integer> it = Client.requests.keySet().iterator();
while (it.hasNext()) {
req = Client.requests.get(it.next());
if (req.node.equals(node) && req.file_id == file_id && hits(req.sbyte, req.length, sbyte, length)) {
return true;
}
}
return false;
}
}
So I basically get errors for the ScheduledEvent class's isRequested method. Since there are more than 100 concurrent threads, I think the error caused by the fact that other threads are using Client.doSynchronized() while other threads try to iterate the request object in isRequested method. Is there any way to make the thread to access that object synchronized without using blocking (Thread.join() etc.)?