|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Aug 2004
Città: Palermo
Messaggi: 1079
|
Sincronizzazione lettura/scrittura su più file tra thread in java
Salve a tutti ho scritto 3 classi che hanno il compito di manipolare un file xml.
Ho una classe che sfrutta sax per leggere dal documento xml; una classe che usa JDom per scrivere sul documento xml; una classe che racchiude le altre due in modo da sincronizzare eventuali accessi al file xml. Quest'ultima classe è quella che viene utilizzata da un server concorrente e che speravo mi permettesse di realizzare la sincronizzazione, solo che ho problemi non indifferenti nel capire come realizzare questa sincronizzazione dato che io non vedo direttamente il file ma solo le due classi per leggere e scrivere su. La classe che ho scritto per la sincronizzazione è questa: Codice:
import java.util.Vector; public class ReadWriteXML { private XMLSaxReaderStat xsrs; //Classe che usa SAX per leggere file XML private XMLJDomWriterStat xjws;// Classe che usa JDOM per scrivere su file XML private String file; public final int STATISTICS_FAILED=555; public final int STATISTICS_SUCCESSFUL=666; public ReadWriteXML(String file){ this.file=file; xsrs = new XMLSaxReaderStat(file); xjws = new XMLJDomWriterStat(file); } public synchronized void changeFile(String file){ this.file=file; xsrs = new XMLSaxReaderStat(file); xjws = new XMLJDomWriterStat(file); } public synchronized int getNumberOfRequest(String method){ Vector<String> risultati= xsrs.getData(method, "numberOfRequest"); return Integer.parseInt(risultati.get(0)); } public synchronized int getNumberOfFailedRequest(String method){ Vector<String> risultati= xsrs.getData(method, "numberOfFailedRequest"); return Integer.parseInt(risultati.get(0)); } public synchronized int getNumberOfSuccessfulRequest(String method){ Vector<String> risultati= xsrs.getData(method, "numberOfSuccessfulRequest"); return Integer.parseInt(risultati.get(0)); } public synchronized void updateStatistics(String method, int tipoAgg, long ttot){ if(tipoAgg==STATISTICS_SUCCESSFUL) xjws.updateStatistic(method, xjws.RESPONSE_TIME, xjws.NUMBER_OF_SUCCESSFULL_REQUEST, (int)ttot); else xjws.updateStatistic(method, xjws.RESPONSE_TIME, xjws.NUMBER_OF_FAILED_REQUEST, (int)ttot); } } Un'altra domanda, io ho a disposizione più file xml da cui posso leggere e che posso scrivere in contemporanea ma come ho scritto la classe non credo che venga gestita anche questa eventualità dato che forse sarebbe meglio inizializzare in ogni metodo le classi per la lettura e la scrittura di un file o sbaglio? Come potrei sincronizzare l'intera classe in modo che vi si acceda uno alla volta? Grazie a tutti, ciao ciao. |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:27.