내 프로그램을 제대로 작동 시키는데 어려움을 겪고 있습니다. 간단히 말해, 내 프로그램은 Main, MessageReceiver, Scheduler (Quartz 패키지 사용), Scheduler 스레드에 의해 스케줄 된 두 가지 유형의 스레드 (TraceReader 및 ScheduledEvent)와 같은 몇 가지 초기 스레드로 구성됩니다. 이제 TraceReader가 시작되면 특수 추적 파일을 읽고 시작 시간, 반복 간격 (500ms ~ 1 초) 및 종료 시간으로 이벤트를 예약합니다. 현재, 약 140 개의 이벤트가 동시에 발생하여 여러 ConcurrentModificationException 오류가 발생합니다. 이제 몇 가지 코드 : ConcurrentModificationExceptions을 피하기위한 Java 동기화?
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;
}
}
그래서 나는 기본적으로 ScheduledEvent 클래스의 isRequested 방법에 대한 오류를 얻을. 100 개 이상의 동시 스레드가 있기 때문에 다른 스레드가 isRequested 메서드에서 요청 개체를 반복하려고 시도하는 동안 다른 스레드가 Client.doSynchronized()를 사용하고 있다는 사실 때문에 오류가 발생했다고 생각합니다. 차단 (Thread.join() 등)을 사용하지 않고 해당 객체에 액세스 할 수있는 스레드를 만들 수있는 방법이 있습니까?
두 가지 스타일 포인트 : 요청의 구성원은 '비공개'이어야하고 게터와 설정자가 추가되어야합니다. 요청은 '비공개'여야합니다. 왜'정적'메소드를 사용하고 계십니까? 그들은 당신이 멋지고 확장 가능한 무언가를 만드는 데 도움이되지 않을 것입니다 .. – pjp
주셔서 감사합니다. Java 프로그래밍에 익숙하지 않고 이러한 키워드의 기능을 이해하지 못합니다. 정적 메소드의 대부분을 가지고있는 이유는 이전에 "정적이 아닌 메소드의 정적 액세스"오류가 발생했기 때문입니다. – Azimuth
요청을 비공개로 선언 할 경우 다른 클래스 (예 : ScheduledEvent)에서 액세스 할 수 없습니다. 그렇지 않습니까? – Azimuth