Ich versuche, ein Programm zu machen, das ein Vokabular aus einer Datei .txt erstellt. Es nimmt eine Datei, erstellt ein Dokument (eine Zeichenkette), teilt dieses Dokument in Sätze auf und schließlich führt es für jeden Satz eine Operation durch (Tokenisierung, Pos-Tagging, Syntaxanalyse von Costitenten usw.).Zum nächsten Index in einer for-Schleife mit Thread nach einer bestimmten Zeit
Irgendwann stoppt das Programm auf einen Satz (das ist schlecht für mich), also was ich tun würde, ist zu vermeiden, auf einem Satz zu bleiben Zeit (nehmen wir an, 120s). Wenn ja, sollte das Programm zum nächsten Satz in der Liste gehen.
Ich dachte, einen Thread und einen Timer zu verwenden und zu überprüfen, ob diese Zeit verstrichen ist, aber ich bin verwirrt, wie man den Thread benutzt.
Hier ist ein Teil meines Codes:
public class Vocabulary {
private Thread thread;
private long sentenceDuration;
private Queue<File> corpus;
public Vocabulary(){
this.sentenceDuration = 0;
corpus = loadCorpus();
}
public void buildVocabulary(){
for (File f : corpus) {
Scanner in = new Scanner(new FileReader(f.getPath()));
// Create a string representing the document
String document = "";
while (in.hasNextLine()) {
document += in.nextLine() + " ";
}
in.close();
// Split the document in sentences
ArrayList<String> sentences = tpr.documentToSentences(document);
for (int i = 0; i < sentences.size(); i++) {
Timer timer = new Timer(1000, timer());
timer.start();
while(sentenceDuration < 120){ // while it takes under 120 seconds to compute the sentence, ok.
List<CoreLabel> tokens = tpr.tokenize(sentences.get(i)); // tokenize the sentence
Tree parse = tpr.apply(tokens); // create the costituents tree
Object[] basicDependencies = tpr.getBasicDependencies(parse); // get the basic dependencies from the tree
// some operations here...
Thread.sleep(1000); // 1000 ms
}
// when it takes over 120 seconds to compute the sentence, jump to the next sentence
System.out.println("Hey, I took long time to compute the sentence. I'm going to the next one!");
}
// Other operations here...
}
}
public void start() {
end();
this.thread = new Thread(this);
this.thread.start();
}
public void end() {
if (thread != null && thread.isAlive()) {
thread.interrupt();
}
}
private ActionListener timer() {
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sentenceDuration++;
}
};
return taskPerformer
}
}
Im Haupt nenne ich einfach:
public static void main(String[] args) {
new Vocabulary().start();
}
Wie kann ich auf das Programm sagen: „wenn 120s übergeben werden, den Satz überspringen“ . Das heißt, "verlassen Sie die While-Schleife sofort! Egal, welche Operation Sie gerade machen." ?
Vielen Dank!
Danke .. das ist die Art und Weise, die ich nehme. Aber kann ich dich bitten, mir ein Beispiel zu geben, das deine Antwort erweitert? Ich habe Semaphoren nie benutzt. –
Ich werde meine Antwort aktualisieren, wenn ich zu einer IDE komme, damit ich Ihnen ein funktionierendes Beispiel geben kann – SpaceghostAli
Wie ich verstanden habe, dient tryAcquire dazu, eine resocurce zu erwerben, und so tryAcquire (120) einfach 120 s warten, bevor die Anfrage geht. join() ist wahrscheinlich, was ich brauche ... der Thread muss seine Operationen ausführen, und wenn 120 verstrichen ist, muss er sterben! Ihre Antwort ist einfach und prägnant. Vielen Dank! –